Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Change controller generator template #70

Merged
merged 1 commit into from

4 participants

@MarceloCajueiro

Hey,

I changed the controller generator template to be more like rails 4 template, with some abstractions and using strong parameters.

@MarceloCajueiro MarceloCajueiro Change controller generator template
Now it is more like the rails 4 controller template, using
before_(filter|action) to set the instance variable @resource and with
strong parameters.
4111fb5
@carlosantoniodasilva
Collaborator

Seems good, thanks mate.

@carlosantoniodasilva carlosantoniodasilva merged commit fdbcb0c into plataformatec:master
@lucasmazza lucasmazza commented on the diff
lib/generators/rails/responders_controller_generator.rb
@@ -22,6 +22,26 @@ def orm_instance_update(params)
orm_instance.update_attributes params
end
end
+
+ def controller_before_filter
+ if defined?(ApplicationController) && ApplicationController.respond_to?(:before_action)
@lucasmazza Owner

Isn't better to check against ActionController::Base and drop the defined? call? :smile:

@rafaelfranca Owner

:+1:

Indeed!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@rafaelfranca rafaelfranca commented on the diff
lib/generators/rails/templates/controller.rb
((23 lines not shown))
@<%= orm_instance.destroy %>
respond_with(@<%= file_name %>)
end
+
+ private
+ def set_<%= file_name %>
@rafaelfranca Owner

:rage: hate this convention.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 9, 2013
  1. @MarceloCajueiro

    Change controller generator template

    MarceloCajueiro authored
    Now it is more like the rails 4 controller template, using
    before_(filter|action) to set the instance variable @resource and with
    strong parameters.
This page is out of date. Refresh to see the latest.
View
22 lib/generators/rails/responders_controller_generator.rb
@@ -22,6 +22,26 @@ def orm_instance_update(params)
orm_instance.update_attributes params
end
end
+
+ def controller_before_filter
+ if defined?(ApplicationController) && ApplicationController.respond_to?(:before_action)
@lucasmazza Owner

Isn't better to check against ActionController::Base and drop the defined? call? :smile:

@rafaelfranca Owner

:+1:

Indeed!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ "before_action"
+ else
+ "before_filter"
+ end
+ end
+
+ def attributes_params
+ if strong_parameters_defined?
+ "#{file_name}_params"
+ else
+ "params[:#{file_name}]"
+ end
+ end
+
+ def strong_parameters_defined?
+ defined?(ActionController::StrongParameters)
+ end
end
end
-end
+end
View
27 lib/generators/rails/templates/controller.rb
@@ -1,5 +1,7 @@
<% module_namespacing do -%>
class <%= controller_class_name %>Controller < ApplicationController
+ <%= controller_before_filter %> :set_<%= file_name %>, only: [:show, :edit, :update, :destroy]
+
<% unless options[:singleton] -%>
def index
@<%= table_name %> = <%= orm_class.all(class_name) %>
@@ -8,7 +10,6 @@ def index
<% end -%>
def show
- @<%= file_name %> = <%= orm_class.find(class_name, "params[:id]") %>
respond_with(@<%= file_name %>)
end
@@ -18,25 +19,37 @@ def new
end
def edit
- @<%= file_name %> = <%= orm_class.find(class_name, "params[:id]") %>
end
def create
- @<%= file_name %> = <%= orm_class.build(class_name, "params[:#{file_name}]") %>
+ @<%= file_name %> = <%= orm_class.build(class_name, attributes_params) %>
<%= "flash[:notice] = '#{class_name} was successfully created.' if " if flash? %>@<%= orm_instance.save %>
respond_with(@<%= file_name %>)
end
def update
- @<%= file_name %> = <%= orm_class.find(class_name, "params[:id]") %>
- <%= "flash[:notice] = '#{class_name} was successfully updated.' if " if flash? %>@<%= orm_instance_update("params[:#{file_name}]") %>
+ <%= "flash[:notice] = '#{class_name} was successfully updated.' if " if flash? %>@<%= orm_instance_update(attributes_params) %>
respond_with(@<%= file_name %>)
end
def destroy
- @<%= file_name %> = <%= orm_class.find(class_name, "params[:id]") %>
@<%= orm_instance.destroy %>
respond_with(@<%= file_name %>)
end
+
+ private
+ def set_<%= file_name %>
@rafaelfranca Owner

:rage: hate this convention.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ @<%= file_name %> = <%= orm_class.find(class_name, "params[:id]") %>
+ end
+ <%- if strong_parameters_defined? -%>
+
+ def <%= "#{file_name}_params" %>
+ <%- if attributes_names.empty? -%>
+ params[:<%= file_name %>]
+ <%- else -%>
+ params.require(:<%= file_name %>).permit(<%= attributes_names.map { |name| ":#{name}" }.join(', ') %>)
+ <%- end -%>
+ end
+ <%- end -%>
end
-<% end -%>
+<% end -%>
Something went wrong with that request. Please try again.