Module: Effective::CrudController
- Extended by:
- ActiveSupport::Concern
- Defined in:
- app/controllers/concerns/effective/crud_controller.rb
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
-
#collection_post_action(action) ⇒ Object
No attributes are assigned or saved.
- #create ⇒ Object
- #destroy ⇒ Object
- #edit ⇒ Object
- #index ⇒ Object
-
#member_actions_for(obj) ⇒ Object
Here we look at all available (class level) member actions, see which ones apply to the current resource This feeds into the helper simple_form_submit(f) Returns a Hash of {data-disable-with: ‘Saving…’, ‘Approve’: ‘Approve’}.
-
#member_post_action(action) ⇒ Object
No attributes are assigned or saved.
- #new ⇒ Object
- #show ⇒ Object
- #update ⇒ Object
Instance Method Details
#collection_post_action(action) ⇒ Object
No attributes are assigned or saved. We purely call action! on the resource
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 |
# File 'app/controllers/concerns/effective/crud_controller.rb', line 272 def collection_post_action(action) action = action.to_s.gsub('bulk_', '').to_sym raise 'expected post, patch or put http action' unless (request.post? || request.patch? || request.put?) raise "expected #{resource_name} to respond to #{action}!" if resources.to_a.present? && !resources.first.respond_to?("#{action}!") successes = 0 resource_klass.transaction do successes = resources.select do |resource| begin resource.public_send("#{action}!") if EffectiveResources.(self, action, resource) rescue => e false end end.length end render json: { status: 200, message: "Successfully #{action_verb(action)} #{successes} / #{resources.length} selected #{resource_plural_name}" } end |
#create ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'app/controllers/concerns/effective/crud_controller.rb', line 165 def create self.resource ||= resource_scope.new @page_title ||= "New #{resource_name.titleize}" EffectiveResources.(self, :create, resource) action = commit_action[:action] EffectiveResources.(self, action, resource) unless action == :save resource.assign_attributes(send(resource_params_method_name)) resource.created_by ||= current_user if resource.respond_to?(:created_by=) if save_resource(resource, action) flash[:success] ||= flash_success(resource, action) redirect_to(resource_redirect_path) else flash.now[:danger] ||= flash_danger(resource, action) render :new end end |
#destroy ⇒ Object
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'app/controllers/concerns/effective/crud_controller.rb', line 224 def destroy self.resource = resource_scope.find(params[:id]) @page_title ||= "Destroy #{resource}" EffectiveResources.(self, :destroy, resource) if resource.destroy flash[:success] ||= flash_success(resource, :delete) else flash[:danger] ||= flash_danger(resource, :delete) end # Make sure we didn't come from a member action that we no longer exist at if referer_redirect_path && !request.referer.to_s.include?("/#{resource.to_param}/") redirect_to(referer_redirect_path) else redirect_to(resource_redirect_path) end end |
#edit ⇒ Object
195 196 197 198 199 200 201 202 |
# File 'app/controllers/concerns/effective/crud_controller.rb', line 195 def edit self.resource ||= resource_scope.find(params[:id]) @page_title ||= "Edit #{resource}" EffectiveResources.(self, :edit, resource) run_callbacks(:resource_render) end |
#index ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'app/controllers/concerns/effective/crud_controller.rb', line 139 def index @page_title ||= resource_plural_name.titleize EffectiveDatatables.(self, :index, resource_klass) self.resources ||= resource_scope.all if resource_datatable_class @datatable ||= resource_datatable_class.new(self, resource_datatable_attributes) end run_callbacks(:resource_render) end |
#member_actions_for(obj) ⇒ Object
Here we look at all available (class level) member actions, see which ones apply to the current resource This feeds into the helper simple_form_submit(f) Returns a Hash of {data-disable-with: ‘Saving…’, ‘Approve’: ‘Approve’}
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 |
# File 'app/controllers/concerns/effective/crud_controller.rb', line 301 def member_actions_for(obj) actions = (self.class.member_actions.presence || self.class._default_member_actions) actions.select do |commit, args| args[:class] = args[:class].to_s (args.key?(:if) ? obj.instance_exec(&args[:if]) : true) && (args.key?(:unless) ? !obj.instance_exec(&args[:unless]) : true) end.sort do |(commit_x, x), (commit_y, y)| # Sort to front primary = (y[:class].include?('primary') ? 1 : 0) - (x[:class].include?('primary') ? 1 : 0) primary = nil if primary == 0 # Sort to back danger = (x[:class].include?('danger') ? 1 : 0) - (y[:class].include?('danger') ? 1 : 0) danger = nil if danger == 0 primary || danger || actions.keys.index(commit_x) <=> actions.keys.index(commit_y) end.inject({}) do |h, (commit, args)| h[commit] = args.except(:action, :if, :unless, :redirect); h end end |
#member_post_action(action) ⇒ Object
No attributes are assigned or saved. We purely call action! on the resource.
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
# File 'app/controllers/concerns/effective/crud_controller.rb', line 245 def member_post_action(action) raise 'expected post, patch or put http action' unless (request.post? || request.patch? || request.put?) if save_resource(resource, action) flash[:success] ||= flash_success(resource, action) redirect_to(referer_redirect_path || resource_redirect_path) else flash.now[:danger] ||= flash_danger(resource, action) if resource_edit_path && (referer_redirect_path || '').end_with?(resource_edit_path) @page_title ||= "Edit #{resource}" render :edit elsif resource_new_path && (referer_redirect_path || '').end_with?(resource_new_path) @page_title ||= "New #{resource_name.titleize}" render :new elsif resource_show_path && (referer_redirect_path || '').end_with?(resource_show_path) @page_title ||= resource_name.titleize render :show else @page_title ||= resource.to_s flash[:danger] = flash.now[:danger] redirect_to(referer_redirect_path || resource_redirect_path) end end end |
#new ⇒ Object
152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'app/controllers/concerns/effective/crud_controller.rb', line 152 def new self.resource ||= resource_scope.new self.resource.assign_attributes( params.to_unsafe_h.except(:controller, :action).select { |k, v| resource.respond_to?("#{k}=") } ) @page_title ||= "New #{resource_name.titleize}" EffectiveResources.(self, :new, resource) run_callbacks(:resource_render) end |
#show ⇒ Object
186 187 188 189 190 191 192 193 |
# File 'app/controllers/concerns/effective/crud_controller.rb', line 186 def show self.resource ||= resource_scope.find(params[:id]) @page_title ||= resource.to_s EffectiveResources.(self, :show, resource) run_callbacks(:resource_render) end |
#update ⇒ Object
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'app/controllers/concerns/effective/crud_controller.rb', line 204 def update self.resource ||= resource_scope.find(params[:id]) @page_title = "Edit #{resource}" EffectiveResources.(self, :update, resource) action = commit_action[:action] EffectiveResources.(self, action, resource) unless action == :save resource.assign_attributes(send(resource_params_method_name)) if save_resource(resource, action) flash[:success] ||= flash_success(resource, action) redirect_to(resource_redirect_path) else flash.now[:danger] ||= flash_danger(resource, action) render :edit end end |