Class: Tasks::CollectingEvents::Parse::Stepwise::DatesController

Inherits:
ApplicationController
  • Object
show all
Includes:
TaskControllerConfiguration
Defined in:
app/controllers/tasks/collecting_events/parse/stepwise/dates_controller.rb

Instance Method Summary (collapse)

Methods included from TaskControllerConfiguration

#set_is_task_controller

Instance Method Details

- (Object) collecting_event_id_param (protected)



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'app/controllers/tasks/collecting_events/parse/stepwise/dates_controller.rb', line 168

def collecting_event_id_param
  retval = nil
  begin
    retval = params.require(:collecting_event_id)
  rescue ActionController::ParameterMissing
    # nothing provided, get the first possible one
    # return CollectingEvent.with_project_id(sessions_current_project_id).order(:id).limit(1).pluck(:id)[0]
    retval = Queries::CollectingEventDatesExtractorQuery.new(
        collecting_event_id: nil,
        filters: parse_filters(params))
                 .all
                 .with_project_id(sessions_current_project_id)
                 .order(:id)
                 .limit(1)
                 .pluck(:id)[0]
  end
  retval
end

- (Object) collecting_event_params (protected)



217
218
219
220
# File 'app/controllers/tasks/collecting_events/parse/stepwise/dates_controller.rb', line 217

def collecting_event_params
  # params.require(:collecting_event).permit(:verbatim_date, :start_date_day, :start_date_month, :start_date_year, :end_date_day, :end_date_month, :end_date_year)
  params.permit(:verbatim_date, :piece, :start_date, :end_date)
end

- (Object) convert



114
115
116
117
# File 'app/controllers/tasks/collecting_events/parse/stepwise/dates_controller.rb', line 114

def convert
  retval = {}
  render json: retval
end

- (Object) convert_params (protected)



222
223
224
# File 'app/controllers/tasks/collecting_events/parse/stepwise/dates_controller.rb', line 222

def convert_params
  params.permit(:include_values, :verbatim_date, :piece, :start_date, :end_date)
end

- (Object) current_collecting_event (protected)



200
201
202
203
204
205
206
207
# File 'app/controllers/tasks/collecting_events/parse/stepwise/dates_controller.rb', line 200

def current_collecting_event
  finding = collecting_event_id_param
  if finding.nil?
    CollectingEvent.first
  else
    CollectingEvent.find(finding)
  end
end

- (Object) index

GET



5
6
7
8
9
10
11
12
13
# File 'app/controllers/tasks/collecting_events/parse/stepwise/dates_controller.rb', line 5

def index
  @filters = parse_filters(params)
  @collecting_event = current_collecting_event
  if @collecting_event.nil?
    flash['notice'] = 'No collecting events with parsable records found.'
    redirect_to hub_path and return
  end
  @matching_items = []
end

- (Object) next_collecting_event_id (protected)

TODO: deprecate for valud from view/helper



188
189
190
191
192
193
194
195
196
197
198
# File 'app/controllers/tasks/collecting_events/parse/stepwise/dates_controller.rb', line 188

def next_collecting_event_id
  filters = parse_filters(params)
  Queries::CollectingEventDatesExtractorQuery.new(
      collecting_event_id: collecting_event_id_param,
      filters: filters)
      .all
      .with_project_id(sessions_current_project_id)
      .order(:id)
      .limit(1)
      .pluck(:id)[0]
end

- (Object) parse_filters(params)



15
16
17
18
19
20
21
22
# File 'app/controllers/tasks/collecting_events/parse/stepwise/dates_controller.rb', line 15

def parse_filters(params)
  # default filter is is all filters
  if params['filters'].blank?
    Utilities::Dates::REGEXP_DATES.keys
  else
    params.permit(filters: [])[:filters].map(&:to_sym)
  end
end

- (Object) process_params (protected)



209
210
211
# File 'app/controllers/tasks/collecting_events/parse/stepwise/dates_controller.rb', line 209

def process_params
  params.permit(:matched_ids, :button, :matched_latitude, :matched_longitude, selected: [])
end

- (Object) regex_on_data(match_data_0) (protected)



158
159
160
161
# File 'app/controllers/tasks/collecting_events/parse/stepwise/dates_controller.rb', line 158

def regex_on_data(match_data_0)
  # retval = "verbatim_label ~* '(\\D#{sql_fix_tix(match_data_0)}\\D)'" ### originally. but replaced below
  retval = "verbatim_label ~ '(\\A#{sql_fix_tix(match_data_0)}\\D)|(\\D#{sql_fix_tix(match_data_0)}\\D)|(\\D#{sql_fix_tix(match_data_0)}\\Z)|(\\A#{sql_fix_tix(match_data_0)}\\Z)'"
end

- (Object) save_selected

all buttons come here, so we first have to look at the button value



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'app/controllers/tasks/collecting_events/parse/stepwise/dates_controller.rb', line 68

def save_selected
  next_id = next_collecting_event_id
  selected = params[:selected]
  if selected.blank?
    message = 'Nothing to save.'
    success = false
  else
    any_failed = false
    selected.each {|item_id|
      ce = CollectingEvent.find(item_id)
      unless ce.nil?
        start_date = convert_params[:start_date].split(' ')
        end_date = convert_params[:end_date].split(' ')
        start_date_year = start_date[0]
        start_date_month = start_date[1]
        start_date_day = start_date[2]
        end_date_year = end_date[0]
        end_date_month = end_date[1]
        end_date_day = end_date[2]
        if ce.update_attributes({start_date_year: start_date[0],
                                 start_date_month: start_date[1],
                                 start_date_day: start_date[2],
                                 end_date_year: end_date[0],
                                 end_date_month: end_date[1],
                                 end_date_day: end_date[2],
                                 verbatim_date: convert_params['verbatim_date']
                                })
        else
          any_failed = true
          message = 'one or more of the collecting events.'
        end
      end
    }
    success = any_failed ? false : true
  end
  if success
    flash['notice'] = 'Updated.' # unless no_flash # if we skipped, there is no flash
  else
    flash['alert'] = message
    next_id = current_collecting_event.id
  end
  # where do we go from here?
  redirect_to dates_index_task_path(collecting_event_id: next_id,
                                    filters: parse_filters(params))
end

- (Object) similar_labels



119
120
121
122
123
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
149
150
151
152
153
154
# File 'app/controllers/tasks/collecting_events/parse/stepwise/dates_controller.rb', line 119

def similar_labels
  retval = {}
  start_date = similar_params[:start_date]
  end_date = similar_params[:end_date]
  piece = similar_params[:piece]
  collecting_event_id = similar_params[:collecting_event_id]
  include_values = (similar_params[:include_values].nil?) ? false : true
  method = [similar_params[:method].to_sym]
  where_clause = regex_on_data(piece)
  where_clause += ' and (verbatim_date is null)' unless include_values
  if method[0] == :undefined # where verbatim label is entered without selecting a result and its method
    selected_items = CollectingEvent.where(where_clause)
                         .with_project_id(sessions_current_project_id)
                         .order(:id)
                         .where.not(id: collecting_event_id).distinct
  else
    selected_items = Queries::CollectingEventDatesExtractorQuery.new(
        collecting_event_id: nil,
        filters: method)
                         .all
                         .with_project_id(sessions_current_project_id)
                         .order(:id)
                         .where.not(id: collecting_event_id)
                         .where(where_clause).distinct
  end

  retval[:count] = selected_items.count.to_s
  retval[:table] = render_to_string(partial: 'make_dates_matching_table',
                                    locals: {
                                        piece: piece,
                                        start_date: start_date,
                                        end_date: end_date,
                                        selected_items: selected_items
                                    })
  render(json: retval)
end

- (Object) similar_params (protected)



213
214
215
# File 'app/controllers/tasks/collecting_events/parse/stepwise/dates_controller.rb', line 213

def similar_params
  params.permit(:include_values, :piece, :start_date, :end_date, :method, :collecting_event_id)
end

- (Object) skip



24
25
26
27
# File 'app/controllers/tasks/collecting_events/parse/stepwise/dates_controller.rb', line 24

def skip
  redirect_to dates_index_task_path(collecting_event_id: next_collecting_event_id,
                                    filters: parse_filters(params))
end

- (Object) sql_fix_tix(item) (protected)



163
164
165
166
# File 'app/controllers/tasks/collecting_events/parse/stepwise/dates_controller.rb', line 163

def sql_fix_tix(item)
  retval = item.gsub("'", "''")
  retval
end

- (Object) update

POST



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/controllers/tasks/collecting_events/parse/stepwise/dates_controller.rb', line 30

def update
  next_id = next_collecting_event_id
  ce = current_collecting_event
  any_failed = false
  # if ce.update_attributes(collecting_event_params)
  start_date = convert_params[:start_date].split(' ')
  end_date = convert_params[:end_date].split(' ')
  start_date_year = start_date[0]
  start_date_month = start_date[1]
  start_date_day = start_date[2]
  end_date_year = end_date[0]
  end_date_month = end_date[1]
  end_date_day = end_date[2]
  if ce.update_attributes({start_date_year: start_date[0],
                           start_date_month: start_date[1],
                           start_date_day: start_date[2],
                           end_date_year: end_date[0],
                           end_date_month: end_date[1],
                           end_date_day: end_date[2],
                           verbatim_date: convert_params['verbatim_date']
                          })
  else
    any_failed = true
    message = 'Failed to update the collecting event.'
  end

  if any_failed
    flash['alert'] = message
    next_id = current_collecting_event.id
  else # a.k.a. success
    flash['notice'] = 'Updated.'
  end
  # where do we go from here?
  redirect_to dates_index_task_path(collecting_event_id: next_id,
                                    filters: parse_filters(params))
end