Permalink
Browse files

finetune to_json, to_xml

  • Loading branch information...
1 parent 67a4078 commit b93acd3f2c482373e8b1dcc7e8751211c4521dcb @mkristian committed Oct 14, 2011
@@ -6,12 +6,42 @@ class <%= class_name %> < <%= parent_class_name.classify %>
<%= attribute.type %> :<%= attribute.name %>
<% end -%>
<% if options[:modified_by] -%>
- belongs_to :modified_by, :class_name => "<%= options[:modified_by].classify %>"
+ belongs_to :modified_by, :class_name => "User"
validates :modified_by_id, :presence => true
<% end -%>
<% if options[:singleton] -%>
def self.instance
self.first || self.new
end
<% end -%>
+<% unless options[:singleton] -%>
+
+ def self.options
+ {
+<% except = []
+ except = [:created_at, :updated_at] if options[:timestamps]
+ except << :modified_by_id if options[:modified_by] -%>
+ :except => <%= except.inspect %>
+ }
+ end
+<% end -%>
+
+ def self.single_options
+ {
+<% if options[:modified_by] -%>
+ :except => [:modified_by_id],
+<% end -%>
+<% if options[:modified_by] || attributes.select {|attr| attr.reference? }.size > 0 -%>
+ :include => {
+<% if options[:modified_by] -%>
+ :modified_by => {
+ :only => [:id, :login, :name]
+ }<% end -%><% attributes.select {|attr| attr.reference? }.each do |attribute| -%>,
+ :<%= attribute.name %> => {
+ :except => [:created_at, :updated_at, :modified_by_id]
+ }<% end %>
+ }
+<% end -%>
+ }
+ end
end
@@ -1,5 +1,3 @@
-<% 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
@@ -12,8 +10,20 @@ def cleanup_params
model.delete :id
<% if options[:timestamps] -%>
model.delete :created_at
-<% unless options[:optimistic] -%>
- model.delete :updated_at
+ <% if options[:optimistic] -%>params[:updated_at] = <% end -%>model.delete :updated_at
+<% if options[:optimistic] -%>
+ end
+
+ def stale?
+ if @<%= singular_table_name %>.nil?
+ @<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %>
+ respond_to do |format|
+ format.html { render :action => "edit" }
+ format.xml { render :xml => nil, :status => :conflict }
+ format.json { render :json => nil, :status => :conflict }
+ end
+ true
+ end
<% end -%>
<% end -%>
end
@@ -28,8 +38,8 @@ def index
respond_to do |format|
format.html # index.html.erb
- format.xml { render :xml => @<%= plural_table_name %> }
- format.json { render :json => @<%= plural_table_name %> }
+ format.xml { render :xml => @<%= plural_table_name %>.to_xml(<%= class_name %>.options) }
+ format.json { render :json => @<%= plural_table_name %>.to_json(<%= class_name %>.options) }
end
end
@@ -41,8 +51,8 @@ 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 %><% if fields -%>.to_json(:methods => [<%= fields %>])<% end -%> }
+ format.xml { render :xml => @<%= singular_table_name %>.to_xml(<%= class_name %>.single_options) }
+ format.json { render :json => @<%= singular_table_name %>.to_json(<%= class_name %>.single_options) }
end
end
@@ -68,8 +78,8 @@ def create
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 %><% if fields -%>.to_json(:methods => [<%= fields %>])<% end -%>, :status => :created, :location => @<%= singular_table_name %> }
+ format.xml { render :xml => @<%= singular_table_name %>.to_xml(<%= class_name %>.single_options), :status => :created, :location => @<%= singular_table_name %> }
+ format.json { render :json => @<%= singular_table_name %>.to_json(<%= class_name %>.single_options), :status => :created, :location => @<%= singular_table_name %> }
else
format.html { render :action => "new" }
format.xml { render :xml => @<%= orm_instance.errors %>, :status => :unprocessable_entity }
@@ -83,30 +93,23 @@ def create
# PUT <%= route_url %>/1.json
def update
<% if options[:optimistic] && options[:timestamps] -%>
- @<%= singular_table_name %> = <%= orm_class.find(class_name, "(params[:#{singular_table_name}]||[]).delete(:updated_at), params[:id]").sub(/\.(get|find)/, '.optimistic_\1') %>
+ @<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:updated_at], params[:id]").sub(/\.(get|find)/, '.optimistic_\1') %>
- if @<%= singular_table_name %>.nil?
- @<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %>
- respond_to do |format|
- format.html { render :action => "edit" }
- format.xml { render :xml => nil, :status => :conflict }
- format.json { render :json => nil, :status => :conflict }
- end
- return
- end
+ return if stale?
<% else -%>
@<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %>
<% end -%>
<% if options[:modified_by] -%>
+
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 %><% if fields -%>.to_json(:methods => [<%= fields %>])<% end -%> }
+ format.xml { render :xml => @<%= singular_table_name %>.to_xml(<%= class_name %>.single_options) }
+ format.json { render :json => @<%= singular_table_name %>.to_json(<%= class_name %>.single_options) }
else
format.html { render :action => "edit" }
format.xml { render :xml => @<%= orm_instance.errors %>, :status => :unprocessable_entity }
@@ -120,17 +123,9 @@ def update
# DELETE <%= route_url %>/1.json
def destroy
<% if options[:optimistic] && options[:timestamps] -%>
- @<%= singular_table_name %> = <%= orm_class.find(class_name, "(params[:#{singular_table_name}]||[]).delete(:updated_at), params[:id]").sub(/\.(get|find)/, '.optimistic_\1') %>
+ @<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:updated_at], params[:id]").sub(/\.(get|find)/, '.optimistic_\1') %>
- if @<%= singular_table_name %>.nil?
- @<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %>
- respond_to do |format|
- format.html { render :action => "edit" }
- format.xml { render :xml => nil, :status => :conflict }
- format.json { render :json => nil, :status => :conflict }
- end
- return
- end
+ return if stale?
<% else -%>
@<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %>
<% end -%>
@@ -9,8 +9,20 @@ def cleanup_params
model = params[:<%= singular_table_name %>] || []
<% if options[:timestamps] -%>
model.delete :created_at
-<% unless options[:optmistic] -%>
- model.delete :updated_at
+ <% if options[:optimistic] -%>params[:updated_at] = <% end -%>model.delete :updated_at
+<% if options[:optimistic] -%>
+ end
+
+ def stale?
+ if @<%= singular_table_name %>.nil?
+ @<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %>
+ respond_to do |format|
+ format.html { render :action => "edit" }
+ format.xml { render :xml => nil, :status => :conflict }
+ format.json { render :json => nil, :status => :conflict }
+ end
+ true
+ end
<% end -%>
<% end -%>
end
@@ -25,8 +37,8 @@ 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.xml { render :xml => @<%= singular_table_name %>.to_xml(<%= class_name %>.single_options) }
+ format.json { render :json => @<%= singular_table_name %>.to_json(<%= class_name %>.single_options) }
end
end
@@ -39,17 +51,24 @@ def edit
# PUT <%= route_url %>.xml
# PUT <%= route_url %>.json
def update
+<% if options[:optimistic] && options[:timestamps] -%>
+ @<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:updated_at], #{class_name}.instance.id").sub(/\.(get|find)/, '.optimistic_\1') %>
+
+ return if stale?
+<% else -%>
@<%= singular_table_name %> = <%= class_name %>.instance
-<% orm_class.find(class_name)
- if options[:modified_by] -%>
- @<%= singular_table_name %>.modified_by = current_user
+<% end -%>
+<% if options[:modified_by] -%>
+
+ 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 %>_path, :notice => '<%= human_name %> was successfully updated.') }
- format.xml { render :xml => @<%= singular_table_name %> }
- format.json { render :json => @<%= singular_table_name %> }
+ format.xml { render :xml => @<%= singular_table_name %>.to_xml(<%= class_name %>.single_options) }
+ format.json { render :json => @<%= singular_table_name %>.to_json(<%= class_name %>.single_options) }
else
format.html { render :action => "edit" }
format.xml { render :xml => @<%= orm_instance.errors %>, :status => :unprocessable_entity }

0 comments on commit b93acd3

Please sign in to comment.