Module: Effective::CrudController::Actions

Included in:
Effective::CrudController
Defined in:
app/controllers/concerns/effective/crud_controller/actions.rb

Instance Method Summary collapse

Instance Method Details

#collection_action(action) ⇒ Object



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'app/controllers/concerns/effective/crud_controller/actions.rb', line 208

def collection_action(action)
  Rails.logger.info 'Processed by Effective::CrudController#collection_action'

  action = action.to_s.gsub('bulk_', '').to_sym

  if params[:ids].present?
    self.resources ||= resource_scope.where(id: params[:ids])
  end

  if request.get? && effective_resource.scope?(action)
    self.resources ||= resource_scope.public_send(action)
  end

  self.resources ||= resource_scope.all

  EffectiveResources.authorize!(self, action, resource_klass)
  @page_title ||= "#{action.to_s.titleize} #{resource_human_name}"

  if request.get?
    @datatable = resource_datatable()
    run_callbacks(:resource_render)

    view = lookup_context.template_exists?(action, _prefixes) ? action : :index
    render(view, locals: { action: action })
    return
  end

  raise "expected all #{resource_name} objects to respond to #{action}!" if resources.to_a.present? && !resources.all? { |resource| resource.respond_to?("#{action}!") }

  successes = 0

  # No attributes are assigned or saved. We purely call action! on the resource

  ActiveRecord::Base.transaction do
    successes = resources.select do |resource|
      begin
        resource.public_send("#{action}!") if EffectiveResources.authorized?(self, action, resource)
      rescue Exception => e
        false
      end
    end.length
  end

  render json: { status: 200, message: "Successfully #{action_verb(action)} #{successes} / #{resources.length} selected #{resource_human_plural_name.downcase}" }
end

#createObject



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
# File 'app/controllers/concerns/effective/crud_controller/actions.rb', line 68

def create
  Rails.logger.info 'Processed by Effective::CrudController#create'

  self.resource ||= resource_scope.new
  action = (commit_action[:action] == :save ? :create : commit_action[:action])

  if respond_to?(:current_user) && resource.respond_to?(:current_user=)
    resource.current_user ||= current_user
  end

  if respond_to?(:current_user) && resource.respond_to?(:created_by=)
    resource.created_by ||= current_user
  end

  resource.assign_attributes(send(resource_params_method_name))

  EffectiveResources.authorize!(self, action, resource)
  @page_title ||= "New #{resource_human_name}"

  if save_resource(resource, action)
    respond_with_success(resource, action)
  else
    respond_with_error(resource, action)
  end
end

#destroyObject



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'app/controllers/concerns/effective/crud_controller/actions.rb', line 154

def destroy
  Rails.logger.info 'Processed by Effective::CrudController#destroy'

  if params[:ids].present?
    return collection_action(:destroy)
  end

  self.resource = resource_scope.find(params[:id])
  action = :destroy

  EffectiveResources.authorize!(self, action, resource)
  @page_title ||= "Destroy #{resource}"

  if save_resource(resource, action)
    respond_with_success(resource, action)
  else
    respond_with_error(resource, action)
  end
end

#editObject



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'app/controllers/concerns/effective/crud_controller/actions.rb', line 111

def edit
  Rails.logger.info 'Processed by Effective::CrudController#edit'

  self.resource ||= resource_scope.find(params[:id])

  EffectiveResources.authorize!(self, :edit, resource)
  @page_title ||= "Edit #{resource}"

  run_callbacks(:resource_render)

  respond_to do |format|
    format.html { }
    format.js { render('edit', formats: :js) }
  end

end

#indexObject



5
6
7
8
9
10
11
12
13
14
15
# File 'app/controllers/concerns/effective/crud_controller/actions.rb', line 5

def index
  Rails.logger.info 'Processed by Effective::CrudController#index'

  EffectiveResources.authorize!(self, :index, resource_klass)
  @page_title ||= resource_human_plural_name

  self.resources ||= resource_scope.all if resource_scope.respond_to?(:all)
  @datatable = resource_datatable()

  run_callbacks(:resource_render)
end

#member_action(action) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'app/controllers/concerns/effective/crud_controller/actions.rb', line 174

def member_action(action)
  Rails.logger.info "Processed by Effective::CrudController#member_action"

  self.resource ||= resource_scope.find(params[:id])

  EffectiveResources.authorize!(self, action, resource)
  @page_title ||= "#{action.to_s.titleize} #{resource}"

  if request.get?
    run_callbacks(:resource_render)

    respond_to do |format|
      format.html { }
      format.js do
        html_template = action if template_present?(action, format: :html)
        template = template_present?(action) ? action : 'member_action'

        render(template, formats: :js, locals: { action: action, html_template: html_template })
      end
    end

    return
  end

  to_assign = (send(resource_params_method_name) rescue {})
  resource.assign_attributes(to_assign) if to_assign.present? && to_assign.permitted?

  if save_resource(resource, action)
    respond_with_success(resource, action)
  else
    respond_with_error(resource, action)
  end
end

#newObject



17
18
19
20
21
22
23
24
25
26
27
28
29
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
66
# File 'app/controllers/concerns/effective/crud_controller/actions.rb', line 17

def new
  Rails.logger.info 'Processed by Effective::CrudController#new'

  self.resource ||= resource_scope.new

  # Assign attributes from the query string
  to_assign = if view_context.respond_to?(:inline_datatable?) && view_context.inline_datatable?
    EffectiveDatatables.find(params[:_datatable_id], params[:_datatable_attributes]).attributes
  end

  to_assign ||= if params[resource_name].present? && params[resource_name].respond_to?(:to_unsafe_h)
    params[resource_name].to_unsafe_h
  end

  to_assign ||= if params.present?
    params.to_unsafe_h.except(:controller, :action, :id, :duplicate_id)
  end

  if to_assign.present?
    resource.assign_attributes(to_assign.select { |k, v| resource.respond_to?("#{k}=") })
  end

  # Duplicate if possible
  if params[:duplicate_id].present?
    duplicate = resource_scope.find(params[:duplicate_id])

    EffectiveResources.authorize!(self, :show, duplicate)

    self.resource = duplicate_resource(duplicate)

    unless resource.kind_of?(resource_scope.klass) && resource.new_record?
      raise "expected duplicate_resource to return an unsaved new #{resource_scope.klass} resource"
    end

    if (message = flash[:success].to_s).present?
      flash.delete(:success)
      flash.now[:success] = "#{message.chomp('.')}. Adding another #{resource_human_name} based on previous."
    end
  end

  EffectiveResources.authorize!(self, :new, resource)
  @page_title ||= "New #{resource_human_name}"

  run_callbacks(:resource_render)

  respond_to do |format|
    format.html { }
    format.js { render('new', formats: :js) }
  end
end

#showObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'app/controllers/concerns/effective/crud_controller/actions.rb', line 94

def show
  Rails.logger.info 'Processed by Effective::CrudController#show'

  self.resource ||= resource_scope.find(params[:id])

  EffectiveResources.authorize!(self, :show, resource)
  @page_title ||= resource.to_s

  run_callbacks(:resource_render)

  respond_to do |format|
    format.html { }
    format.js { render('show', formats: :js) }
  end

end

#updateObject



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
# File 'app/controllers/concerns/effective/crud_controller/actions.rb', line 128

def update
  Rails.logger.info 'Processed by Effective::CrudController#update'

  self.resource ||= resource_scope.find(params[:id])
  action = (commit_action[:action] == :save ? :update : commit_action[:action])

  EffectiveResources.authorize!(self, action, resource)
  @page_title ||= "Edit #{resource}"

  if respond_to?(:current_user) && resource.respond_to?(:current_user=)
    resource.current_user ||= current_user
  end

  if respond_to?(:current_user) && resource.respond_to?(:updated_by=)
    resource.updated_by ||= current_user
  end

  resource.assign_attributes(send(resource_params_method_name))

  if save_resource(resource, action)
    respond_with_success(resource, action)
  else
    respond_with_error(resource, action)
  end
end