Module: Effective::CrudController::ClassMethods

Defined in:
app/controllers/concerns/effective/crud_controller.rb

Instance Method Summary collapse

Instance Method Details

#after_error(*names, &blk) ⇒ Object



38
39
40
# File 'app/controllers/concerns/effective/crud_controller.rb', line 38

def after_error(*names, &blk)
  _insert_callbacks(names, blk) { |name, options| set_callback(:resource_error, :after, name, options) }
end

#after_save(*names, &blk) ⇒ Object



34
35
36
# File 'app/controllers/concerns/effective/crud_controller.rb', line 34

def after_save(*names, &blk)
  _insert_callbacks(names, blk) { |name, options| set_callback(:resource_save, :after, name, options) }
end

#before_render(*names, &blk) ⇒ Object



30
31
32
# File 'app/controllers/concerns/effective/crud_controller.rb', line 30

def before_render(*names, &blk)
  _insert_callbacks(names, blk) { |name, options| set_callback(:resource_render, :before, name, options) }
end

#collection_action(action) ⇒ Object

Defines a function to handle a GET and POST request on this URL Handles bulk_ actions Just add a member action to your routes, you shouldn’t need to call this directly You shouldn’t need to call this directly



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'app/controllers/concerns/effective/crud_controller.rb', line 133

def collection_action(action)
  define_method(action) do
    if params[:ids].present?
      self.resources ||= resource_scope.where(id: params[:ids])
    end

    if 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_plural_name.titleize}"

    collection_post_action(action) unless request.get?
  end
end

#define_actions_from_routesObject

Automatically respond to any action defined via the routes file



23
24
25
26
27
# File 'app/controllers/concerns/effective/crud_controller.rb', line 23

def define_actions_from_routes
  resource = Effective::Resource.new(controller_path)
  resource.member_actions.each { |action| member_action(action) }
  resource.collection_actions.each { |action| collection_action(action) }
end

#member_action(action) ⇒ Object

Defines a function to handle a GET and POST request on this URL Just add a member action to your routes, you shouldn’t need to call this directly



117
118
119
120
121
122
123
124
125
126
127
# File 'app/controllers/concerns/effective/crud_controller.rb', line 117

def member_action(action)
  define_method(action) do
    self.resource ||= resource_scope.find(params[:id])

    EffectiveResources.authorize!(self, action, resource)

    @page_title ||= "#{action.to_s.titleize} #{resource}"

    member_post_action(action) unless request.get?
  end
end

#page_title(label = nil, opts = {}, &block) ⇒ Object

page_title ‘My Title’, only: [:new]



84
85
86
87
88
89
90
91
92
# File 'app/controllers/concerns/effective/crud_controller.rb', line 84

def page_title(label = nil, opts = {}, &block)
  raise 'expected a label or block' unless (label || block_given?)

  instance_exec do
    before_action(opts) do
      @page_title ||= (block_given? ? instance_exec(&block) : label)
    end
  end
end

#resource_scope(obj = nil, opts = {}, &block) ⇒ Object

Return value should be: a Relation: Thing.where(user: current_user) a Hash: { user_id: current_user.id }



105
106
107
108
109
110
111
112
113
# File 'app/controllers/concerns/effective/crud_controller.rb', line 105

def resource_scope(obj = nil, opts = {}, &block)
  raise 'expected a proc or block' unless (obj.respond_to?(:call) || block_given?)

  instance_exec do
    before_action(opts) do
      @_effective_resource_scope ||= instance_exec(&(block_given? ? block : obj))
    end
  end
end

#submit(action, commit = nil, args = {}) ⇒ Object

This controls the form submit options of effective_submit It also controls the redirect path for any actions

Effective::Resource will populate this with all member_post_actions And you can control the details with this DSL:

submit :approve, ‘Save and Approve’, unless: -> { approved? }, redirect: :show

submit :toggle, ‘Blacklist’, if: -> { sync? }, class: ‘btn btn-primary’ submit :toggle, ‘Whitelist’, if: -> { !sync? }, class: ‘btn btn-primary’ submit :save, ‘Save’, success: -> { “#self was saved okay!” }



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/controllers/concerns/effective/crud_controller.rb', line 54

def submit(action, commit = nil, args = {})
  raise 'expected args to be a Hash or false' unless args.kind_of?(Hash) || args == false

  if commit == false
    submits.delete_if { |commit, args| args[:action] == action }; return
  end

  if args == false
    submits.delete(commit); return
  end

  if commit # Overwrite the default member action when given a custom commit
    submits.delete_if { |commit, args| args[:default] && args[:action] == action }
  end

  if args.key?(:if) && args[:if].respond_to?(:call) == false
    raise "expected if: to be callable. Try submit :approve, 'Save and Approve', if: -> { finished? }"
  end

  if args.key?(:unless) && args[:unless].respond_to?(:call) == false
    raise "expected unless: to be callable. Try submit :approve, 'Save and Approve', unless: -> { declined? }"
  end

  redirect = args.delete(:redirect_to) || args.delete(:redirect) # Remove redirect_to keyword. use redirect.
  args.merge!(action: action, redirect: redirect)

  (submits[commit] ||= {}).merge!(args)
end