Skip to content

Commit

Permalink
text entry allowed
Browse files Browse the repository at this point in the history
  • Loading branch information
ndvan committed Jul 25, 2019
1 parent 9c160f0 commit a069924
Show file tree
Hide file tree
Showing 9 changed files with 146 additions and 0 deletions.
60 changes: 60 additions & 0 deletions app/controllers/text_entries_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
class TextEntriesController < ApplicationController
def new
@text_entry = TextEntry.new
end

def create
@text_entry = current_or_guest_user.text_entries.build(text_entry_params)
if @text_entry.save
name = params[:name].empty? ? "Text entry" : params[:name]

@list = @text_entry.create_list(name: name, description: params[:description])
flash[:success] = @list.name + " list is created!"

entry = text_entry_params["corpus"].gsub("\n", " ")
extracted_response = Req.get( Rails.configuration.x.sv_GNRD_wrapper_text + entry )

if extracted_response.empty?
extracted_response = Req.get(Rails.configuration.x.sv_TaxonFinder_wrapper_text + entry)
end

@list.update_attributes(extracted: extracted_response.to_json)

resolved_response = Req.post( Rails.configuration.x.sv_OToL_TNRS_wrapper,
extracted_response.to_json,
:content_type => :json )

if resolved_response.empty?
extracted_for_gnr = extracted_response
extracted_for_gnr["fuzzy_match"] = true
extracted_for_gnr["multiple_match"] = false
resolved_response = Req.post( Rails.configuration.x.sv_GNR_TNRS_wrapper,
extracted_for_gnr.to_json,
:content_type => :json )
end

if resolved_response.empty?
error = Req.post( Rails.configuration.x.sv_OToL_TNRS_wrapper,
extracted_response.to_json,
{:content_type => :json},
true )
fail_record = Failure.last.id
end

@list.update_attributes(resolved: resolved_response.to_json,
resolving_error: error,
possible_failure_record: fail_record)

redirect_to list_path(@list)
else
render action: "new"
end
end

private

def text_entry_params
params.require(:text_entry).permit(:corpus)
end

end
9 changes: 9 additions & 0 deletions app/models/text_entry.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class TextEntry < ApplicationRecord
has_one :list, as: :resource, dependent: :destroy
belongs_to :user

default_scope -> { order(created_at: :desc) }

validates :user_id, presence: true
validates :corpus, presence: true
end
5 changes: 5 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class User < ApplicationRecord
# :trackable, :validatable

validates :email, presence: true
has_many :text_entries, dependent: :destroy
has_many :links, dependent: :destroy
has_many :documents, dependent: :destroy
has_many :onpls, dependent: :destroy
Expand All @@ -17,6 +18,10 @@ class User < ApplicationRecord

def lists
lists = []

text_entry_resource_lists = self.text_entries.map {|l| l.list}
lists.concat text_entry_resource_lists

online_resource_lists = self.links.map {|l| l.list}
lists.concat online_resource_lists

Expand Down
1 change: 1 addition & 0 deletions app/views/lists/_new_list.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
</h3>
</div>
<div class="list-group center">
<%= link_to "Extract from text entry", new_text_entry_path, class:"list-group-item" %>
<%= link_to "Extract from web", new_link_path, class:"list-group-item" %>
<%= link_to "Extract from doc", new_document_path, class:"list-group-item" %>
<!-- Button trigger modal -->
Expand Down
44 changes: 44 additions & 0 deletions app/views/text_entries/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<% provide(:title, "Create trees from text entry") %>

<div class="container">
<h1>Extract species names from text entry</h1>
<%= form_for @text_entry, html: {class: "form-horizontal"} do |f| %>

<div class="row">
<div class="col-sm-6 col-sm-offset-3">
<%= render 'shared/error_messages', object: f.object %>
</div>
</div>

<div class="form-group">
<%= f.label :corpus, for: "corpus", class: "col-sm-3 control-label" do %>
<span data-toggle="tooltip" title="Required field"><%= fa_icon "asterisk", text: "Input text" %></span>
<% end %>
<div class="col-sm-7">
<%= f.text_area :corpus, placeholder: "For better accuracy, use colon or space for seperator. E.g. Canis lupus pallipes, Panthera pardus, Melursus ursinus, Macaca mulatta", class: "form-control", rows: 8 %>
</div>
</div>

<div class="form-group">
<%= label_tag :name, for: "name", class: "col-sm-3 control-label" do %>
Name for list
<% end %>
<div class="col-sm-4">
<%= text_field_tag :name, nil, placeholder: "Name", class: "form-control" %>
</div>
</div>

<div class="form-group">
<%= label_tag :description, "Description", for: "description_", class: "col-sm-3 control-label" %>
<div class="col-sm-7">
<%= text_area :description, "", placeholder: "Description", class: "form-control", rows: 5 %>
</div>
</div>

<div class="form-group">
<div class="col-sm-offset-3 col-sm-9">
<%= f.submit "Submit", class: "btn btn-default btn-lg btn-primary" %>
</div>
</div>
<% end %>
</div>
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
end
end

resources :text_entries, only: [:new, :create]
resources :links, only: [:new, :create]
resources :documents, only: [:new, :create]
resources :onpls, only: [:new, :create]
Expand Down
10 changes: 10 additions & 0 deletions db/migrate/20190725154455_create_text_entries.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateTextEntries < ActiveRecord::Migration[5.1]
def change
create_table :text_entries do |t|
t.text :corpus
t.references :user, foreign_key: true

t.timestamps
end
end
end
9 changes: 9 additions & 0 deletions test/fixtures/text_entries.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

one:
corpus: MyText
user: one

two:
corpus: MyText
user: two
7 changes: 7 additions & 0 deletions test/models/text_entry_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class TextEntryTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

0 comments on commit a069924

Please sign in to comment.