Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added form_builder config

  • Loading branch information...
commit b9e1f3ea435190c19b4ed89c2aa3d90b69e914d2 1 parent 54c6270
@crosebrugh crosebrugh authored
View
1  Gemfile
@@ -35,6 +35,7 @@ group :development, :test do
gem 'cancan' if ENV['AUTHORIZATION_ADAPTER'] == 'cancan'
gem 'factory_girl', '2.0.0.beta2'
gem 'generator_spec'
+ gem 'ruby-debug19'
end
gemspec
View
32 README.md
@@ -544,6 +544,7 @@ column, you can:
### Create and update views
+* Form rendering
* Field groupings
* Visibility
* Labels
@@ -557,6 +558,37 @@ column, you can:
* Overriding field help texts
* CKEditor integration
+**Form rendering**
+
+RailsAdmin renders these views with Rails' form builder (form_for). If you want to use a different
+form builder then provide an override for the edit view or independingly for the
+create and update views. The argument is a symbol or string that is sent to the view
+to process the form. This is handy for integrating things like the nested form builder (https://github.com/ryanb/nested_form) if you need to override a field's edit template.
+
+ RailsAdmin.config do |config|
+ config.model Team do
+ edit do
+ form_builder :nested_form_for
+ field :name
+ end
+ end
+ end
+
+or independently
+
+ RailsAdmin.config do |config|
+ config.model Team do
+ create do
+ form_builder :create_form_for
+ field :name
+ end
+ update do
+ form_builder :update_form_for
+ field :name
+ end
+ end
+ end
+
**Field groupings**
By default RailsAdmin groups fields in the edit views (create and update views)
View
2  app/views/rails_admin/main/_form_filtering_multiselect.html.erb
@@ -16,7 +16,7 @@
<div class="field <%= field.dom_id %>">
<%= form.label field.method_name, field.label %>
<input type="hidden" name="<%= field.dom_name %>" value=""/>
- <%= form.select field.method_name, collection, { :selected => selected_ids }, field.html_attributes %>
+ <%= form.select field.method_name, collection, { :selected => selected_ids, :object => @object }, field.html_attributes %>
<% if field.has_errors? %>
<span class="errorMessage"><%= "#{field.label} #{field.errors.first}" %></span>
<% end %>
View
2  app/views/rails_admin/main/edit.html.erb
@@ -19,7 +19,7 @@
</ul>
</div>
<%= render(:partial => 'layouts/rails_admin/flash', :locals => {:flash => flash}) -%>
- <%= form_for(@object, :url => rails_admin_update_path(:model_name => @abstract_model.to_param, :id => @object.id), :html => { :method => "put", :multipart => true }) do |form| %>
+ <%= send(@model_config.update.form_builder, @object, :url => rails_admin_update_path(:model_name => @abstract_model.to_param, :id => @object.id), :html => { :method => "put", :multipart => true }) do |form| %>
<% @model_config.edit.with(:object => @object).visible_groups.each do |fieldset| %>
<%= render :partial => 'form_fieldset', :locals => { :fieldset => fieldset, :form => form, :object => @object } -%>
<% end %>
View
4 app/views/rails_admin/main/new.html.erb
@@ -7,11 +7,11 @@
</div>
<div class="ra-block-content">
<%= render(:partial => 'layouts/rails_admin/flash', :locals => {:flash => flash}) -%>
- <%= form_for(@object, :url => rails_admin_create_path(:model_name => @abstract_model.to_param, :id => @object.id), :html => { :multipart => true }) do |form| %>
+ <%= send(@model_config.create.form_builder, @object, :url => rails_admin_create_path(:model_name => @abstract_model.to_param, :id => @object.id), :html => { :multipart => true }) do |form| %>
<% @model_config.create.with(:object => @object).visible_groups.each do |fieldset| %>
<%= render :partial => 'form_fieldset', :locals => { :fieldset => fieldset, :form => form, :object => @object } -%>
<% end %>
<%= render :partial => 'submit_buttons' %>
<% end %>
</div>
- </div>
+ </div>
View
8 lib/rails_admin/config/sections/update.rb
@@ -43,6 +43,14 @@ def initialize(parent)
end
end
end
+
+ # Should be a method that's called as bindings[:view].send(@model_config.update.form_builder,...)
+ # e.g.: bindings[:view].send(form_for,...) do |form|
+ # form.text_field(...)
+ # end
+ register_instance_option(:form_builder) do
+ :form_for
+ end
end
end
end
View
151 spec/requests/config/edit/rails_admin_config_edit_spec.rb
@@ -649,4 +649,155 @@ def color_enum
response.should have_tag("input.color")
end
end
+
+ describe "Form builder configuration" do
+
+ it "should allow override of default" do
+ RailsAdmin.config do |config|
+ config.model Player do
+ edit do
+ field :name
+ end
+ end
+ config.model Team do
+ edit do
+ form_builder :form_for_edit
+ field :name
+ end
+ end
+ config.model Fan do
+ create do
+ form_builder :form_for_create
+ field :name
+ end
+ update do
+ form_builder :form_for_update
+ field :name
+ end
+ end
+ config.model League do
+ create do
+ form_builder :form_for_league_create
+ field :name
+ end
+ update do
+ field :name
+ end
+ end
+ end
+
+ RailsAdmin::Config.model(Player).create.form_builder.should be(:form_for)
+ RailsAdmin::Config.model(Player).update.form_builder.should be(:form_for)
+ RailsAdmin::Config.model(Player).edit.form_builder.should be(:form_for)
+
+ RailsAdmin::Config.model(Team).update.form_builder.should be(:form_for_edit)
+ RailsAdmin::Config.model(Team).create.form_builder.should be(:form_for_edit)
+ RailsAdmin::Config.model(Team).edit.form_builder.should be(:form_for_edit)
+
+ RailsAdmin::Config.model(Fan).create.form_builder.should be(:form_for_create)
+ RailsAdmin::Config.model(Fan).update.form_builder.should be(:form_for_update)
+ RailsAdmin::Config.model(Fan).edit.form_builder.should be(:form_for_update) # not sure we care
+
+ RailsAdmin::Config.model(League).create.form_builder.should be(:form_for_league_create)
+ RailsAdmin::Config.model(League).update.form_builder.should be(:form_for)
+ RailsAdmin::Config.model(League).edit.form_builder.should be(:form_for) # not sure we care
+
+ # don't spill over into other views
+ expect {
+ RailsAdmin::Config.model(Team).list.form_builder
+ }.to raise_error(NoMethodError,/undefined method/)
+ end
+
+ it "should be used in the new and edit views" do
+ TF_CREATE_OUTPUT = "MY TEST FORM CREATE TEXT FIELD"
+ TF_UPDATE_OUTPUT = "MY TEST FORM UPDATE TEXT FIELD"
+
+ module MyCreateForm
+ class Builder < ::ActionView::Helpers::FormBuilder
+ def text_field(*args)
+ TF_CREATE_OUTPUT
+ end
+ end
+
+ module ViewHelper
+ def create_form_for(*args, &block)
+ options = args.extract_options!.reverse_merge(:builder => MyCreateForm::Builder)
+ form_for(*(args << options), &block)
+ end
+ end
+ end
+
+ module MyUpdateForm
+ class Builder < ::ActionView::Helpers::FormBuilder
+ def text_field(*args)
+ TF_UPDATE_OUTPUT
+ end
+ end
+
+ module ViewHelper
+ def update_form_for(*args, &block)
+ options = args.extract_options!.reverse_merge(:builder => MyUpdateForm::Builder)
+ form_for(*(args << options), &block)
+ end
+ end
+ end
+
+ class ActionView::Base
+ include MyCreateForm::ViewHelper
+ include MyUpdateForm::ViewHelper
+ end
+
+ RailsAdmin.config do |config|
+ config.model Player do
+ edit do
+ field :name
+ end
+ end
+ config.model Team do
+ edit do
+ form_builder :create_form_for
+ field :name
+ end
+ end
+ config.model League do
+ create do
+ form_builder :create_form_for
+ field :name
+ end
+ update do
+ form_builder :update_form_for
+ field :name
+ end
+ end
+ end
+
+ get rails_admin_new_path(:model_name => "player")
+ response.should have_tag("input#player_name")
+ response.should_not contain(TF_CREATE_OUTPUT)
+ response.should_not contain(TF_UPDATE_OUTPUT)
+ @player = Factory.create :player
+ get rails_admin_edit_path(:model_name => "player", :id => @player.id)
+ response.should have_tag("input#player_name")
+ response.should_not contain(TF_CREATE_OUTPUT)
+ response.should_not contain(TF_UPDATE_OUTPUT)
+
+ get rails_admin_new_path(:model_name => "team")
+ response.should contain(TF_CREATE_OUTPUT)
+ response.should_not contain(TF_UPDATE_OUTPUT)
+ @team = Factory.create :team
+ get rails_admin_edit_path(:model_name => "team", :id => @team.id)
+ response.should contain(TF_CREATE_OUTPUT)
+ response.should_not contain(TF_UPDATE_OUTPUT)
+
+ get rails_admin_new_path(:model_name => "league")
+ response.should contain(TF_CREATE_OUTPUT)
+ response.should_not contain(TF_UPDATE_OUTPUT)
+ @league = Factory.create :league
+ get rails_admin_edit_path(:model_name => "league", :id => @league.id)
+ response.should_not contain(TF_CREATE_OUTPUT)
+ response.should contain(TF_UPDATE_OUTPUT)
+ end
+
+ end
+
end
Please sign in to comment.
Something went wrong with that request. Please try again.