Permalink
Browse files

Add Edit and Show action and other cool stuff ;)

  • Loading branch information...
1 parent b0feb97 commit 8b9b7fc70b77fce2d80f00331e2f6ed7b8af1141 @brunto committed Oct 11, 2011
View
2 lib/generators/rails_admin/scaffold_controller/scaffold_controller_generator.rb
@@ -39,7 +39,7 @@ def columns_without_id
def available_views
#%w(index edit show new _form)
- %w(index new)
+ %w(index new show edit)
end
end
View
83 lib/generators/rails_admin/scaffold_controller/templates/controller.rb
@@ -2,8 +2,15 @@
class Admin::<%= controller_class_name %>Controller < RailsAdmin::ApplicationController
layout "rails_admin/main"
helper RailsAdmin::ActivoHelper
+
+ before_filter :set_param_model
+ before_filter :get_model, :except => [:index]
+ before_filter :get_object, :only => [:show, :edit, :update, :delete, :destroy]
+ before_filter :get_attributes, :only => [:create, :update]
+ before_filter :check_for_cancel, :only => [:create, :update, :destroy, :export, :bulk_destroy]
def index
+ @page_type = '<%= plural_name %>'
@<%= plural_table_name %> = <%= orm_class.all(class_name) %>
respond_to do |format|
@@ -12,59 +19,101 @@ def index
end
def show
- @<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %>
+ @page_type = '<%= plural_name %>'
+ @object = <%= orm_class.find(class_name, "params[:id]") %>
respond_to do |format|
format.html # show.html.erb
end
end
def new
+ @page_type = '<%= plural_name %>'
@object = <%= orm_class.build(class_name) %>
- params[:model_name] = '<%= singular_table_name %>'
- self.get_model
+
respond_to do |format|
format.html # new.html.erb
end
end
def edit
- @<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %>
+ @page_type = '<%= plural_name %>'
+ @object = <%= orm_class.find(class_name, "params[:id]") %>
+
+ respond_to do |format|
+ format.html # edit.html.erb
+ end
end
def create
+ @page_type = '<%= plural_name %>'
@object = <%= orm_class.build(class_name, "params[:#{singular_table_name}]") %>
-
- params[:model_name] = '<%= singular_table_name %>'
- self.get_model
+
respond_to do |format|
if @object.save
- format.html { redirect_to @object, <%= key_value :notice, "'#{human_name} was successfully created.'" %> }
+ format.html { redirect_to_on_success }
else
- format.html { render <%= key_value :action, '"new"' %> }
+ format.html { render :action => "new" }
end
end
end
def update
- @<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %>
+ @page_type = '<%= plural_name %>'
+ @object = <%= orm_class.find(class_name, "params[:id]") %>
respond_to do |format|
- if @<%= orm_instance.update_attributes("params[:#{singular_table_name}]") %>
- format.html { redirect_to @<%= singular_table_name %>, <%= key_value :notice, "'#{human_name} was successfully updated.'" %> }
+ if @object.update_attributes(params[:<%= singular_table_name %>])
+ format.html { redirect_to_on_success }
else
- format.html { render <%= key_value :action, '"edit"' %> }
+ format.html { render :action => "edit" }
end
end
end
def destroy
- @<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %>
- @<%= orm_instance.destroy %>
+ @object = <%= orm_class.find(class_name, "params[:id]") %>
+ if @object.destroy
+ flash[:notice] = t("admin.flash.successful", :name => @model_config.label, :action => t("admin.actions.deleted"))
+ else
+ flash[:error] = t("admin.flash.error", :name => @model_config.label, :action => t("admin.actions.deleted"))
+ end
- respond_to do |format|
- format.html { redirect_to <%= index_helper %>_url }
+ redirect_to list_path(:model_name => '<%= plural_name %>')
+ end
+
+ private
+
+ def set_param_model
+ params[:model_name] = '<%= plural_name %>'
+ end
+
+ def get_attributes
+ @attributes = params[@abstract_model.to_param.singularize.gsub('~','_')] || {}
+ @attributes.each do |key, value|
+ # Deserialize the attribute if attribute is serialized
+ if @abstract_model.model.serialized_attributes.keys.include?(key) and value.is_a? String
+ @attributes[key] = YAML::load(value)
+ end
+ # Delete fields that are blank
+ @attributes[key] = nil if value.blank?
+ end
+ end
+
+ def redirect_to_on_success
+ notice = t("admin.flash.successful", :name => @model_config.label, :action => t("admin.actions.#{params[:action]}d"))
+ if params[:_add_another]
+ redirect_to new_path(:model_name =>'<%= plural_name %>'), :notice => notice
+ elsif params[:_add_edit]
+ redirect_to edit_path(:model_name =>'<%= plural_name %>', :id => @object.id), :notice => notice
+ else
+ redirect_to list_path(:model_name =>'<%= plural_name %>'), :notice => notice
end
end
+
+ def check_for_cancel
+ redirect_to list_path(:model_name =>'<%= plural_name %>'), :notice => t("admin.flash.noaction") if params[:_continue]
+ end
+
end
<% end -%>
View
29 lib/generators/rails_admin/scaffold_controller/templates/edit.html.erb
@@ -0,0 +1,29 @@
+<%%
+ @page_name = '<%= plural_name.capitalize %>'
+ @model_name = '<%= plural_table_name %>'
+ dashboard_link = link_to(I18n.t("admin.dashboard.name").capitalize, dashboard_path)
+ list_link = link_to(I18n.t("admin.breadcrumbs.list").capitalize, list_path(:model_name => @model_name))
+ show_link = link_to(I18n.t("admin.breadcrumbs.show").capitalize, show_path(:model_name => @model_name, :id => @object.id))
+ current_link = link_to(I18n.t("admin.breadcrumbs.new").capitalize, new_path(:model_name => @model_name, :id => @object.id))
+ links_for_breadcrumb = [ dashboard_link, list_link, show_link, current_link ]
+
+ controlls_links = [
+ action_button(delete_path(@abstract_model, @object.id), t("admin.list.delete_action"), :cross)
+ ]
+%>
+
+<%% activo_block do %>
+ <%%= activo_secondary_navigation(links_for_breadcrumb, current_link) %>
+ <%% activo_block_content do %>
+ <%%= activo_block_content_header(@page_name, controlls_links) %>
+ <%% activo_block_inner_content do %>
+ <%%= render(:partial => 'layouts/rails_admin/flash', :locals => {:flash => flash}) %>
+ <%%= send(@model_config.update.form_builder, @object, :url => update_path(:model_name => @model_name, :id => @object.id), :html => { :method => "put", :multipart => true, :class => "form" }) do |form| %>
+ <%% @model_config.edit.with(:form => form, :object => @object, :view => self).visible_groups.each do |fieldset| %>
+ <%%= render :partial => 'form_fieldset', :locals => { :fieldset => fieldset, :form => form, :object => @object } %>
+ <%% end %>
+ <%%= render :partial => 'submit_buttons' %>
+ <%% end %>
+ <%% end %>
+ <%% end %>
+<%% end %>
View
3 lib/generators/rails_admin/scaffold_controller/templates/index.html.erb
@@ -16,6 +16,7 @@
<%% activo_block_content do %>
<%%= activo_block_content_header(@page_name, controlls_links) %>
<%% activo_block_inner_content do %>
+ <%%= render(:partial => 'layouts/rails_admin/flash', :locals => {:flash => flash}) %>
<%% activo_table do %>
<thead>
<tr>
@@ -38,7 +39,7 @@
<td class="last">
<%%= action_icon(show_path(:model_name => @model_name, :id => object.id), :show, t("admin.list.show_action")) %>
<%%= action_icon(edit_path(:model_name => @model_name, :id => object.id), :edit, t("admin.list.edit_action")) %>
- <%%= action_icon(destroy_path(:model_name => @model_name, :id => object.id), :cross, t("admin.list.delete_action")) %>
+ <%%= action_icon(delete_path(:model_name => @model_name, :id => object.id), :cross, t("admin.list.delete_action")) %>
</td>
</tr>
<%% end %>
View
41 lib/generators/rails_admin/scaffold_controller/templates/show.html.erb
@@ -0,0 +1,41 @@
+<%%
+ @page_name = '<%= plural_name.capitalize %>'
+ @model_name = '<%= plural_table_name %>'
+ dashboard_link = link_to(I18n.t("admin.dashboard.name").capitalize, dashboard_path)
+ list_link = link_to(I18n.t("admin.breadcrumbs.list").capitalize, list_path(:model_name => @model_name))
+ current_link = link_to(I18n.t("admin.breadcrumbs.show").capitalize, show_path(:model_name => @model_name))
+ links_for_breadcrumb = [ dashboard_link, list_link, current_link ]
+
+ controlls_links = [
+ #action_button(history_object_path(:id => @object.id), t("admin.history.name"), :'calendar-hover'),
+ action_button(edit_path(:model_name => @model_name, :id => @object.id), t("admin.list.edit_action"), :edit),
+ action_button(delete_path(@abstract_model, @object.id), t("admin.list.delete_action"), :cross)
+ ]
+%>
+<%% activo_block do %>
+ <%%= activo_secondary_navigation(links_for_breadcrumb, current_link) %>
+ <%% activo_block_content do %>
+ <%%= activo_block_content_header(@page_name, controlls_links) %>
+ <%% activo_block_inner_content do %>
+ <%%= render(:partial => 'layouts/rails_admin/flash', :locals => {:flash => flash}) %>
+ <%% @model_config.show.with(:object => @object, :view => self).visible_groups.each do |fieldset| %>
+ <%% unless (fields = fieldset.with(:object => @object, :view => self).visible_fields).empty? %>
+ <%% if !(values = fields.map{ |f| f.pretty_value.presence }).compact.empty? || !RailsAdmin::config.compact_show_view %>
+ <div class="fieldset">
+ <h4><%%= fieldset.label %></h4>
+ <%%= content_tag :div, fieldset.help, :class => "help" if fieldset.help %>
+ <%% fields.each_with_index do |field, index| %>
+ <%% if (value = values[index]) || !RailsAdmin::config.compact_show_view %>
+ <div class="field <%%= field.abstract_model.to_param.singularize %>_<%%= field.name %>">
+ <div class="label"><%%= field.label %></div>
+ <div class="value"><%%= value || ' - ' %></div>
+ </div>
+ <%% end %>
+ <%% end %>
+ </div>
+ <%% end %>
+ <%% end %>
+ <%% end %>
+ <%% end %>
+ <%% end %>
+<%% end %>

0 comments on commit 8b9b7fc

Please sign in to comment.