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

#add_childrenObject

POST /leads/1/add_children.json



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'app/controllers/leads_controller.rb', line 88

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

  num_to_add.times do
    @lead.children.create!
  end
  expand_lead
  render action: :show, status: :created, location: @lead
end

#api_keyObject



274
275
276
277
# File 'app/controllers/leads_controller.rb', line 274

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



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

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

#createObject

POST /leads POST /leads.json



74
75
76
77
78
79
80
81
82
83
84
85
# File 'app/controllers/leads_controller.rb', line 74

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.



188
189
190
191
192
193
194
195
196
197
198
199
# File 'app/controllers/leads_controller.rb', line 188

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



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'app/controllers/leads_controller.rb', line 124

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
178
179
180
181
182
183
# File 'app/controllers/leads_controller.rb', line 165

def destroy_children
  respond_to do |format|
    if @lead.parent_id.present?
      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
    else
      @lead.errors.add(:destroy, "failed - can't delete the only couplet.")
      format.json {
        render json: @lead.errors, status: :unprocessable_entity
      }
    end
  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



153
154
155
156
157
158
159
160
161
# File 'app/controllers/leads_controller.rb', line 153

def destroy_subtree
  begin
    @lead.transaction_nuke
  rescue ActiveRecord::RecordInvalid
    render json: @lead.errors, status: :unprocessable_entity
  end

  head :no_content
end

#downloadObject

GET /leads/download



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

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



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'app/controllers/leads_controller.rb', line 202

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



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

def edit
  redirect_to new_lead_task_path lead_id: @lead.id
end

#expand_leadObject (private)



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

def expand_lead
  @children = @lead.children
  @futures = @lead.children.map(&:future)
  @ancestors = @lead.ancestors.reverse
end

#indexObject

GET /leads GET /leads.json



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

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



106
107
108
109
# File 'app/controllers/leads_controller.rb', line 106

def insert_couplet
  @lead.insert_couplet
  head :no_content
end

#insert_keyObject

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



221
222
223
224
225
226
227
228
# File 'app/controllers/leads_controller.rb', line 221

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)



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

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



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

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

#newObject

GET /leads/new



63
64
65
# File 'app/controllers/leads_controller.rb', line 63

def new
  redirect_to new_lead_task_path
end

#new_coupletObject (private)



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

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

#otusObject

GET /leads/1/otus.json



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

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

#redirect_option_textsObject

GET /leads/1/redirect_option_texts.json



38
39
40
# File 'app/controllers/leads_controller.rb', line 38

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

#reorder_childrenObject

PATCH /leads/1/reorder_children.json



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

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
  @futures = @leads.map(&:future)
end

#reorder_paramsObject (private)



293
294
295
# File 'app/controllers/leads_controller.rb', line 293

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

#searchObject



256
257
258
259
260
261
262
263
# File 'app/controllers/leads_controller.rb', line 256

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)



281
282
283
# File 'app/controllers/leads_controller.rb', line 281

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

#showObject

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



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

def show
  if @lead.children.present?
    expand_lead
  else
    @children = nil
    @futures = nil
    @ancestors = @lead.ancestors.reverse
  end
end

#show_allObject



54
55
56
# File 'app/controllers/leads_controller.rb', line 54

def show_all
  @key = @lead.all_children
end

#show_all_printObject



58
59
60
# File 'app/controllers/leads_controller.rb', line 58

def show_all_print
  @key = @lead.all_children_standard_key
end

#updateObject

PATCH/PUT /leads/1.json



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

def update
  begin
    @lead.update!(lead_params)
    # Note that future changes when redirect is updated.
    @future = @lead.future
  rescue ActiveRecord::RecordInvalid
    render json: @lead.errors, status: :unprocessable_entity
  end
end