Skip to content
Browse files

* cleanup of inmutable json params

* belongs_to association with migration and proper controller

* modified_by
  • Loading branch information...
1 parent 0cb3744 commit 490b950f769d24f7c65503d0c281eec8e3e03953 @mkristian committed Oct 5, 2011
View
3 lib/generators/rails/active_record/model/model.rb
@@ -6,7 +6,8 @@ class <%= class_name %> < <%= parent_class_name.classify %>
<%= attribute.type %> :<%= attribute.name %>
<% end -%>
<% if options[:modified_by] -%>
- belongs_to :modified_by, :class_name => "<%= options[:user_model] %>"
+ belongs_to :modified_by, :class_name => "<%= options[:modified_by].classify %>"
+ validates :modified_by_id, :presence => true
<% end -%>
<% if options[:singleton] -%>
def self.instance
View
37 lib/generators/rails/scaffold_controller/scaffold_controller/controller.rb
@@ -1,4 +1,25 @@
+<% field_names = attributes.select {|a| a.type == :belongs_to }.collect{ |a| a.name } -%>
+<% fields = ":" + field_names.join(", :") if field_names.size > 0 -%>
class <%= controller_class_name %>Controller < ApplicationController
+
+ before_filter :cleanup_params
+
+ private
+
+ def cleanup_params
+ # compensate the shortcoming of the incoming json/xml
+ model = params[:<%= singular_table_name %>] || []
+ model.delete :id
+<% if options[:timestamps] -%>
+ model.delete :created_at
+<% unless options[:optimistic] -%>
+ model.delete :updated_at
+<% end -%>
+<% end -%>
+ end
+
+ public
+
# GET <%= route_url %>
# GET <%= route_url %>.xml
# GET <%= route_url %>.json
@@ -21,7 +42,7 @@ def show
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @<%= singular_table_name %> }
- format.json { render :json => @<%= singular_table_name %> }
+ format.json { render :json => @<%= singular_table_name %><% if fields -%>.to_json(:methods => [<%= fields %>])<% end -%> }
end
end
@@ -41,14 +62,14 @@ def edit
def create
@<%= singular_table_name %> = <%= orm_class.build(class_name, "params[:#{singular_table_name}]") %>
<% if options[:modified_by] -%>
- @<%= singular_table_name %>.current_user = current_user
+ @<%= singular_table_name %>.modified_by = current_user
<% end -%>
respond_to do |format|
if @<%= orm_instance.save %>
format.html { redirect_to(@<%= singular_table_name %>, :notice => '<%= human_name %> was successfully created.') }
format.xml { render :xml => @<%= singular_table_name %>, :status => :created, :location => @<%= singular_table_name %> }
- format.json { render :json => @<%= singular_table_name %>, :status => :created, :location => @<%= singular_table_name %> }
+ format.json { render :json => @<%= singular_table_name %><% if fields -%>.to_json(:methods => [<%= fields %>])<% end -%>, :status => :created, :location => @<%= singular_table_name %> }
else
format.html { render :action => "new" }
format.xml { render :xml => @<%= orm_instance.errors %>, :status => :unprocessable_entity }
@@ -75,18 +96,17 @@ def update
end
<% else -%>
@<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %>
- (params[:<%= singular_table_name %>]||[]).delete(:updated_at)
<% end -%>
- (params[:<%= singular_table_name %>]||[]).delete(:id)
<% if options[:modified_by] -%>
- @<%= singular_table_name %>.current_user = current_user
+ params[:<%= singular_table_name %>] ||= {}
+ params[:<%= singular_table_name %>][:modified_by] = current_user
<% end -%>
respond_to do |format|
if @<%= orm_instance.update_attributes("params[:#{singular_table_name}]") %>
format.html { redirect_to(@<%= singular_table_name %>, :notice => '<%= human_name %> was successfully updated.') }
format.xml { render :xml => @<%= singular_table_name %> }
- format.json { render :json => @<%= singular_table_name %> }
+ format.json { render :json => @<%= singular_table_name %><% if fields -%>.to_json(:methods => [<%= fields %>])<% end -%> }
else
format.html { render :action => "edit" }
format.xml { render :xml => @<%= orm_instance.errors %>, :status => :unprocessable_entity }
@@ -114,9 +134,6 @@ def destroy
<% else -%>
@<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %>
<% end -%>
-<% if options[:modified_by] -%>
- @<%= singular_table_name %>.current_user = current_user
-<% end -%>
@<%= orm_instance.destroy %>
View
19 lib/generators/rails/scaffold_controller/scaffold_controller/singleton_controller.rb
@@ -1,5 +1,22 @@
class <%= controller_class_name %>Controller < ApplicationController
+ before_filter :cleanup_params
+
+ private
+
+ def cleanup_params
+ # compensate the shortcoming of the incoming json/xml
+ model = params[:<%= singular_table_name %>] || []
+<% if options[:timestamps] -%>
+ model.delete :created_at
+<% unless options[:optmistic] -%>
+ model.delete :updated_at
+<% end -%>
+<% end -%>
+ end
+
+ public
+
# GET <%= route_url %>
# GET <%= route_url %>.xml
# GET <%= route_url %>.json
@@ -25,7 +42,7 @@ def update
@<%= singular_table_name %> = <%= class_name %>.instance
<% orm_class.find(class_name)
if options[:modified_by] -%>
- @<%= singular_table_name %>.current_user = current_user
+ @<%= singular_table_name %>.modified_by = current_user
<% end -%>
respond_to do |format|
View
3 lib/generators/scaffold_controller/scaffold_controller_generator.rb
@@ -6,13 +6,16 @@ module Generators
class ScaffoldControllerGenerator < ::Rails::Generators::NamedBase
include ::Rails::Generators::ResourceHelpers
+ argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
+
check_class_collision :suffix => "Controller"
class_option :orm, :banner => "NAME", :type => :string, :required => true,
:desc => "ORM to generate the controller for"
class_option :singleton, :type => :boolean, :default => false
class_option :optimistic, :type => :boolean, :default => false
+ class_option :modified_by, :type => :string
class_option :timestamps, :type => :boolean, :default => true
def create_controller_files

0 comments on commit 490b950

Please sign in to comment.
Something went wrong with that request. Please try again.