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, #cumulative_projects_created_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, #sound_cumulative_gb_per_year, #sound_gb_per_year, #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

#add_childrenObject

POST /leads/1/add_children.json



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'app/controllers/leads_controller.rb', line 81

def add_children
  num_to_add = params[:num_to_add]
  if num_to_add < 1 || num_to_add > 2
    @lead.errors.add(:add_children,
      "request must be for 1 or 2, was '#{num_to_add}'"
    )
    render json: @lead.errors, status: :unprocessable_entity
    return
  end

  # currently infers num_to_add
  @lead.add_children(sessions_current_project_id, sessions_current_user_id)
  expand_lead
  render action: :show, status: :created, location: @lead
end

#api_keyObject



268
269
270
271
# File 'app/controllers/leads_controller.rb', line 268

def api_key
  @lead = Lead.where(project_id: sessions_current_project_id, is_public: true).find(params.require(:id))
  render '/leads/api/v1/key'
end

#autocompleteObject



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

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

#batch_create_lead_itemsObject

Creates a new key populated with otus from params.



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

def batch_create_lead_items
  @lead = Lead.create!(params.require(:lead).permit(:text))
  @lead.batch_populate_lead_items(params[:otu_query],
    sessions_current_project_id, sessions_current_user_id
  )

  @lead.add_children(sessions_current_project_id, sessions_current_user_id)

  render json: @lead
end

#createObject

POST /leads POST /leads.json



67
68
69
70
71
72
73
74
75
76
77
78
# File 'app/controllers/leads_controller.rb', line 67

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

#delete_childrenObject

For deleting all children at a particular level of the key where at most one of the children has its own children; the grandchildren, if any, are reparented.



182
183
184
185
186
187
188
189
190
191
192
193
# File 'app/controllers/leads_controller.rb', line 182

def delete_children
  respond_to do |format|
    if @lead.destroy_children
      format.json { head :no_content }
    else
      @lead.errors.add(:delete, 'failed!')
      format.json {
        render json: @lead.errors, status: :unprocessable_entity
      }
    end
  end
end

#destroyObject

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



117
118
119
120
121
122
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 117

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
  end

  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 ActiveRecord::RecordInvalid
    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

#destroy_childrenObject

For destroying all children at a particular level of the key where none of the children have their own children.



165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'app/controllers/leads_controller.rb', line 165

def destroy_children
  if @lead.parent_id.present?
    if @lead.destroy_children
      head :no_content
    else
      @lead.errors.add(:delete, 'failed!')
      render json: @lead.errors, status: :unprocessable_entity
    end
  else
    @lead.errors.add(:destroy, "failed - can't delete the only couplet.")
    render json: @lead.errors, status: :unprocessable_entity
  end
end

#destroy_subtreeObject

A separate action from destroy to be called in different contexts: this one can be called on any lead, not just root. POST /leads/1/destroy_subtree.json



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'app/controllers/leads_controller.rb', line 146

def destroy_subtree
  parent = @lead.parent
  begin
    Lead.destroy_lead_and_descendants(
      @lead, sessions_current_project_id, sessions_current_user_id
    )
  rescue TaxonWorks::Error => e
    parent.errors.add(:error, e)
    render json: parent.errors, status: :unprocessable_entity
    return
  end

  @lead = parent.reload
  expand_lead
  render :show, status: :ok, location: @lead
end

#downloadObject

GET /leads/download



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

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.html



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'app/controllers/leads_controller.rb', line 196

def duplicate
  respond_to do |format|
    format.html {
      if !@lead.parent_id
        if @lead.dupe
          flash[:notice] = 'Key cloned.'
        else
          flash[:error] = @lead.errors.full_messages.join('; ')
        end
      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



61
62
63
# File 'app/controllers/leads_controller.rb', line 61

def edit
  redirect_to new_lead_task_path lead_id: @lead.id
end

#expand_leadObject (private)



313
314
315
316
# File 'app/controllers/leads_controller.rb', line 313

def expand_lead
  @children = @lead.children
  @lead_item_otus = @lead.apportioned_lead_item_otus
end

#indexObject

GET /leads GET /leads.json



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

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/insert_couplet.json



98
99
100
101
# File 'app/controllers/leads_controller.rb', line 98

def insert_couplet
  @lead.insert_couplet
  head :no_content
end

#insert_keyObject

POST /leads/1/insert_key.json?key_to_insert=:id



215
216
217
218
219
220
221
222
# File 'app/controllers/leads_controller.rb', line 215

def insert_key
  if !@lead.insert_key(params[:key_to_insert])
    render json: @lead.errors, status: :unprocessable_entity
    return
  end

  head :no_content
end

#lead_paramsObject (private)



301
302
303
304
305
306
307
# File 'app/controllers/leads_controller.rb', line 301

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

#listObject



34
35
36
37
# File 'app/controllers/leads_controller.rb', line 34

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

#newObject

GET /leads/new



56
57
58
# File 'app/controllers/leads_controller.rb', line 56

def new
  redirect_to new_lead_task_path
end

#new_coupletObject (private)



318
319
320
321
322
# File 'app/controllers/leads_controller.rb', line 318

def new_couplet
  2.times do
    @lead.children.create!
  end
end

#otusObject

GET /leads/1/otus.json



239
240
241
# File 'app/controllers/leads_controller.rb', line 239

def otus
  @otus = Otu.associated_with_key(@lead)
end

#redirect_option_textsObject

GET /leads/1/redirect_option_texts.json



40
41
42
# File 'app/controllers/leads_controller.rb', line 40

def redirect_option_texts
  @texts = @lead.redirect_options(sessions_current_project_id)
end

#reorder_childrenObject

PATCH /leads/1/reorder_children.json



225
226
227
228
229
230
231
232
233
234
235
236
# File 'app/controllers/leads_controller.rb', line 225

def reorder_children
  begin
    @lead.reorder_children(reorder_params[:reorder_list])
  rescue TaxonWorks::Error => e
    @lead.errors.add(:reorder_failed, e.to_s)
    render json: @lead.errors, status: :unprocessable_entity
    return
  end

  @leads = @lead.reload.children
  @lead_item_otus = @lead.apportioned_lead_item_otus
end

#reorder_paramsObject (private)



309
310
311
# File 'app/controllers/leads_controller.rb', line 309

def reorder_params
  params.permit(reorder_list: [])
end

#searchObject



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

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)



297
298
299
# File 'app/controllers/leads_controller.rb', line 297

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

#set_observation_matrixObject



285
286
287
288
289
290
291
292
293
# File 'app/controllers/leads_controller.rb', line 285

def set_observation_matrix
  if params[:observation_matrix_id].nil?
    @lead.errors.add(:observation_matrix_id, 'is required.')
    render json: @lead.errors, status: :unprocessable_entity
  end

  @lead.update!(observation_matrix_id: params[:observation_matrix_id])
  head :no_content
end

#showObject

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



46
47
48
49
50
51
52
53
# File 'app/controllers/leads_controller.rb', line 46

def show
  if @lead.children.present?
    expand_lead
  else
    @children = nil
    @lead_item_otus = @lead.apportioned_lead_item_otus
  end
end

#updateObject

PATCH/PUT /leads/1.json



104
105
106
107
108
109
110
111
112
113
# File 'app/controllers/leads_controller.rb', line 104

def update
  begin
    @lead.update!(lead_params)
    @lead_item_otus = @lead.parent_id ?
      @lead.parent.apportioned_lead_item_otus :
      { parent: [], children: [] }
  rescue ActiveRecord::RecordInvalid
    render json: @lead.errors, status: :unprocessable_entity
  end
end