Class: LeadsController

Inherits:
ApplicationController show all
Includes:
DataControllerConfiguration::ProjectDataControllerConfiguration
Defined in:
app/controllers/leads_controller.rb

Constant Summary

Constants included from ProjectsHelper

ProjectsHelper::CLASSIFIER, ProjectsHelper::CLASSIFIER_ANNOTATION

Instance Method Summary collapse

Methods included from DataControllerConfiguration::ProjectDataControllerConfiguration

#annotator_params

Methods included from RedirectHelper

#destroy_redirect

Methods included from RequestType

#json_request?

Methods included from LogRecent

#log_user_recent_route

Methods included from Cookies

#digest_cookie, #digested_cookie_exists?

Methods included from Whitelist

#whitelist_constantize

Methods included from ProjectsHelper

#cumulative_gb_per_year, #document_cumulative_gb_per_year, #document_gb_per_year, #gb_per_year, #image_cumulative_gb_per_year, #image_gb_per_year, #invalid_object, #project_classification, #project_link, #project_matches, #project_tag, #projects_list, #projects_search_form, #taxonworks_classification, #week_in_review_graphs

Methods included from Api::Intercept

#intercept_api

Methods included from TokenAuthentication

#intercept_project, #intercept_user, #intercept_user_or_project, #project_token_authenticate, #token_authenticate

Instance Method Details

#all_textsObject

GET /leads/1/all_texts.json



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/controllers/leads_controller.rb', line 37

def all_texts
  leads = Lead
    .select(:id, :text, :origin_label)
    .with_project_id(sessions_current_project_id)
    .order(:text)
  ancestor_ids = @lead.ancestor_ids
  @texts = leads.filter_map {|o|
    if o.id != @lead.id && !ancestor_ids.include?(o.id)
      {
        id: o.id,
        label: o.origin_label,
        text: o.text.nil? ? '' : o.text.truncate(40)
      }
    end
  }
end

#autocompleteObject



244
245
246
247
248
249
# File 'app/controllers/leads_controller.rb', line 244

def autocomplete
  @leads = ::Queries::Lead::Autocomplete.new(
    params.require(:term),
    project_id: sessions_current_project_id,
  ).autocomplete
end

#createObject

POST /leads POST /leads.json



99
100
101
102
103
104
105
106
107
108
109
110
# File 'app/controllers/leads_controller.rb', line 99

def create
  @lead = Lead.new(lead_params)
  respond_to do |format|
    if @lead.save
      new_couplet # we make two blank couplets so we can show the key
      expand_lead
      format.json {}
    else
      format.json { render json: @lead.errors, status: :unprocessable_entity}
    end
  end
end

#create_for_editObject

POST /leads/1/create_for_edit.json



90
91
92
93
94
95
# File 'app/controllers/leads_controller.rb', line 90

def create_for_edit
  if @lead.children.size == 0
    new_couplet
  end
  expand_lead
end

#delete_coupletObject

For deleting a couplet where one side has no children but the other might; the side with children is reparented.



194
195
196
197
198
199
200
201
202
203
204
205
# File 'app/controllers/leads_controller.rb', line 194

def delete_couplet
  respond_to do |format|
    if @lead.destroy_couplet
      format.json { head :no_content }
    else
      @lead.errors.add(:delete, 'failed - is there a node redirecting to one of these?')
      format.json {
        render json: @lead.errors, status: :unprocessable_entity
      }
    end
  end
end

#destroyObject

DELETE /leads/1 DELETE /leads/1.json



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'app/controllers/leads_controller.rb', line 145

def destroy
  if @lead.parent_id
    respond_to do |format|
      flash[:error] = 'Delete aborted - you can only delete on root nodes.'
      format.html { redirect_back(fallback_location: (request.referer || root_path)) }
      format.json { head :no_content, status: :unprocessable_entity }
    end
    return
  else
    begin
      @lead.transaction_nuke
      respond_to do |format|
        flash[:notice] = 'Key was succesfully destroyed.'
        format.html { destroy_redirect @lead }
        format.json { head :no_content }
      end
    rescue # TODO: add specifics
      respond_to do |format|
        flash[:error] = 'Delete failed!'
        format.html { redirect_back(fallback_location: (request.referer || root_path)) }
        format.json { render json: @lead.errors, status: :unprocessable_entity }
      end
    end
  end
end

#destroy_coupletObject

For destroying a couplet with no children on either side.



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'app/controllers/leads_controller.rb', line 172

def destroy_couplet
  respond_to do |format|
    if @lead.parent_id.present?
      if @lead.destroy_couplet
        format.json { head :no_content }
      else
        @lead.errors.add(:delete, 'failed - is there a node redirecting to one of these?')
        format.json {
          render json: @lead.errors, status: :unprocessable_entity
        }
      end
    else
      @lead.errors.add(:destroy, "failed - can't delete the only couplet.")
      format.json {
        render json: @lead.errors, status: :unprocessable_entity
      }
    end
  end
end

#downloadObject

GET /leads/download



261
262
263
264
265
266
267
# File 'app/controllers/leads_controller.rb', line 261

def download
  send_data Export::CSV.generate_csv(
      Lead.where(project_id: sessions_current_project_id)
    ),
    type: 'text',
    filename: "leads_#{DateTime.now}.tsv"
end

#duplicateObject

POST /leads/1/duplicate



208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'app/controllers/leads_controller.rb', line 208

def duplicate
  respond_to do |format|
    format.html {
      if !@lead.parent_id
        @lead.dupe
        flash[:notice] = 'Key cloned.'
      else
        flash[:error] = 'Clone aborted - you can only clone on a root node.'
      end
      redirect_to action: :list
    }
  end
end

#editObject

GET /leads/1/edit



85
86
87
# File 'app/controllers/leads_controller.rb', line 85

def edit
  redirect_to new_lead_task_path lead_id: @lead.id
end

#expand_leadObject (private)



282
283
284
285
286
287
288
289
# File 'app/controllers/leads_controller.rb', line 282

def expand_lead
  # Assumes the parent node is @lead and @lead has two children
  @left = @lead.children[0]
  @right = @lead.children[1]
  @left_future = @left.future
  @right_future = @right.future
  @parents = @lead.ancestors.reverse
end

#indexObject

GET /leads GET /leads.json



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/controllers/leads_controller.rb', line 9

def index
  respond_to do |format|
    format.html {
      one_week_ago = Time.now.utc.to_date - 7
      @recent_objects = Lead
        .roots_with_data(sessions_current_project_id)
        .where('key_updated_at > ?', one_week_ago)
        .reorder(key_updated_at: :desc)
        .limit(10)

      render '/shared/data/all/index'
    }
    format.json {
      if params[:load_root_otus]
        @leads = Lead.roots_with_data(sessions_current_project_id, true)
      else
        @leads = Lead.roots_with_data(sessions_current_project_id)
      end
    }
  end
end

#insert_coupletObject

POST /leads/1.json/insert_couplet



113
114
115
116
117
118
119
# File 'app/controllers/leads_controller.rb', line 113

def insert_couplet
  @lead.insert_couplet

  respond_to do |format|
    format.json { head :no_content }
  end
end

#lead_params(require = :lead) ⇒ Object (private)



275
276
277
278
279
280
# File 'app/controllers/leads_controller.rb', line 275

def lead_params(require = :lead)
  params.require(require).permit(
    :otu_id, :text, :origin_label, :description, :redirect_id,
    :link_out, :link_out_text, :is_public, :position
  )
end

#listObject



31
32
33
34
# File 'app/controllers/leads_controller.rb', line 31

def list
  @leads = Lead.
    roots_with_data(sessions_current_project_id).page(params[:page])
end

#newObject

GET /leads/new



78
79
80
81
82
# File 'app/controllers/leads_controller.rb', line 78

def new
  respond_to do |format|
    format.html { redirect_to new_lead_task_path }
  end
end

#new_coupletObject (private)



291
292
293
294
295
296
297
298
# File 'app/controllers/leads_controller.rb', line 291

def new_couplet
  # Assumes the parent node is @lead
  return if @lead.children.size > 0
  @left = @lead.children.create!
  @right = @lead.children.create!
  @left_future = @left.future
  @right_future =  @right.future
end

#no_grandchildren(lead) ⇒ Object (private)



300
301
302
303
# File 'app/controllers/leads_controller.rb', line 300

def no_grandchildren(lead)
  children = lead.children
  (children.size == 2) and (children[0].children.size == 0) and (children[1].children.size == 0)
end

#otusObject

GET /leads/1/otus.json



234
235
236
237
238
239
240
241
242
# File 'app/controllers/leads_controller.rb', line 234

def otus
  leads_list = @lead.self_and_descendants.where.not(otu_id: nil).includes(:otu)

  @otus = leads_list.to_a.map{|l| l.otu}.uniq(&:id)

  respond_to do |format|
    format.json {}
  end
end

#searchObject



251
252
253
254
255
256
257
258
# File 'app/controllers/leads_controller.rb', line 251

def search
  if params[:id].blank?
    redirect_to(lead_path,
                alert: 'You must select an item from the list with a click or tab press before clicking show.')
  else
    redirect_to lead_path(params[:id])
  end
end

#set_leadObject (private)



271
272
273
# File 'app/controllers/leads_controller.rb', line 271

def set_lead
  @lead = Lead.find(params[:id])
end

#showObject

GET /leads/1 GET /leads/1.json



56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/controllers/leads_controller.rb', line 56

def show
  children = @lead.children
  if children.size == 2
    expand_lead
  else
    @left = nil
    @right = nil
    @left_future = []
    @right_future = []
    @parents = @lead.ancestors.reverse
  end
end

#show_allObject



69
70
71
# File 'app/controllers/leads_controller.rb', line 69

def show_all
  @key = @lead.all_children
end

#show_all_printObject



73
74
75
# File 'app/controllers/leads_controller.rb', line 73

def show_all_print
  @key = @lead.all_children_standard_key
end

#updateObject

PATCH/PUT /leads/1 PATCH/PUT /leads/1.json



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'app/controllers/leads_controller.rb', line 123

def update
  @lead = Lead.find(params[:lead][:id])
  @left = Lead.find(params[:left][:id])
  @right = Lead.find(params[:right][:id])
  respond_to do |format|
    begin
      @lead.update!(lead_params)
      @left.update!(lead_params(:left))
      @right.update!(lead_params(:right))
      # Note that future changes when redirect is updated.
      expand_lead
      format.json {}
    rescue
      @lead.errors.merge!(@left)
      @lead.errors.merge!(@right)
      format.json { render json: @lead.errors, status: :unprocessable_entity }
    end
  end
end

#update_metaObject

POST /leads/1/update_meta.json



223
224
225
226
227
228
229
230
231
# File 'app/controllers/leads_controller.rb', line 223

def update_meta
  respond_to do |format|
    if @lead.update(lead_params)
      format.json {}
    else
      format.json { render json: @lead.errors, status: :unprocessable_entity}
    end
  end
end