Skip to content

Commit

Permalink
Merge pull request #1566 from neilang/master
Browse files Browse the repository at this point in the history
Add options to hide add/edit buttons on associated fields
  • Loading branch information
bbenezech committed Mar 21, 2013
2 parents fbd764d + 9c937ac commit 8533e5a
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 4 deletions.
Expand Up @@ -41,7 +41,7 @@

- selected_ids = (hdv = field.html_default_value).nil? ? selected_ids : hdv
= form.select field.method_name, collection, { :selected => selected_ids, :object => form.object }, field.html_attributes.reverse_merge({:data => { :filteringmultiselect => true, :options => js_data.to_json }, :multiple => true})
- if authorized? :new, config.abstract_model
- if authorized?(:new, config.abstract_model) && field.inline_add
- path_hash = { :model_name => config.abstract_model.to_param, :modal => true }
- path_hash.merge!({ :associations => { field.inverse_of => (form.object.persisted? ? form.object.id : 'new') } }) if field.inverse_of
= link_to "<i class=\"icon-plus icon-white\"></i> ".html_safe + wording_for(:link, :new, config.abstract_model), '#', :data => { :link => new_path(path_hash) }, :class => "create btn btn-info", :style => 'margin-left:10px'
4 changes: 2 additions & 2 deletions app/views/rails_admin/main/_form_filtering_select.html.haml
Expand Up @@ -28,10 +28,10 @@
- selected_id = (hdv = field.html_default_value).nil? ? selected_id : hdv
= form.select field.method_name, collection, { :selected => selected_id, :include_blank => true }, field.html_attributes.reverse_merge({ :data => { :filteringselect => true, :options => js_data.to_json }, :placeholder => t('admin.misc.search') })

- if authorized? :new, config.abstract_model
- if authorized?(:new, config.abstract_model) && field.inline_add
- path_hash = { :model_name => config.abstract_model.to_param, :modal => true }
- path_hash.merge!({ :associations => { field.inverse_of => (form.object.persisted? ? form.object.id : 'new') } }) if field.inverse_of
= link_to "<i class=\"icon-plus icon-white\"></i> ".html_safe + wording_for(:link, :new, config.abstract_model), '#', :data => { :link => new_path(path_hash) }, :class => "btn btn-info create", :style => 'margin-left:10px'

- if edit_url.present?
- if edit_url.present? && field.inline_edit
= link_to "<i class=\"icon-pencil icon-white\"></i> ".html_safe + wording_for(:link, :edit, config.abstract_model), '#', :data => { :link => edit_url }, :class => "btn btn-info update #{field.value.nil? && 'disabled'}", :style => 'margin-left:10px'
2 changes: 1 addition & 1 deletion app/views/rails_admin/main/_form_nested_many.html.haml
Expand Up @@ -2,7 +2,7 @@
.btn-group
%a.btn.btn-info.toggler{:'data-toggle' => "button", :'data-target' => "#{form.jquery_namespace(field)} > .tab-content, #{form.jquery_namespace(field)} > .controls > .nav", :class => (field.active? ? 'active' : '')}
%i.icon-white
- unless field.nested_form[:update_only]
- unless field.nested_form[:update_only] || !field.inline_add
= form.link_to_add "<i class=\"icon-plus icon-white\"></i> #{wording_for(:link, :new, field.associated_model_config.abstract_model)}".html_safe, field.name, { :class => 'btn btn-info' }
= form.errors_for(field)
= form.help_for(field)
Expand Down
8 changes: 8 additions & 0 deletions lib/rails_admin/config/fields/types/belongs_to_association.rb
Expand Up @@ -23,6 +23,14 @@ class BelongsToAssociation < RailsAdmin::Config::Fields::Association
nested_form ? :form_nested_one : :form_filtering_select
end

register_instance_option :inline_add do
true
end

register_instance_option :inline_edit do
true
end

def selected_id
bindings[:object].send(foreign_key)
end
Expand Down
4 changes: 4 additions & 0 deletions lib/rails_admin/config/fields/types/has_many_association.rb
Expand Up @@ -17,6 +17,10 @@ class HasManyAssociation < RailsAdmin::Config::Fields::Association
false
end

register_instance_option :inline_add do
true
end

def method_name
nested_form ? "#{super}_attributes".to_sym : "#{super.to_s.singularize}_ids".to_sym # name_ids
end
Expand Down
9 changes: 9 additions & 0 deletions lib/rails_admin/config/fields/types/has_one_association.rb
Expand Up @@ -17,6 +17,15 @@ class HasOneAssociation < RailsAdmin::Config::Fields::Association
(o = value) && o.send(associated_model_config.object_label_method)
end

register_instance_option :inline_add do
true
end

register_instance_option :inline_edit do
true
end


def editable?
(nested_form || abstract_model.model.new.respond_to?("#{self.name}_id=")) && super
end
Expand Down
74 changes: 74 additions & 0 deletions spec/integration/config/edit/rails_admin_config_edit_spec.rb
Expand Up @@ -593,6 +593,79 @@ class HelpTest < Tableless
expect(find("#team_division_id_field .help-block")).to have_content("Optional")
expect(find("#team_name_field .help-block")).to have_content("Required")
end

it "can hide the add button on an associated field" do
RailsAdmin.config Player do
edit do
field :team do
inline_add false
end
field :draft do
inline_add false
end
field :comments do
inline_add false
end
end
end
visit new_path(:model_name => "player")
should have_no_selector('a', :text => 'Add a new Team')
should have_no_selector('a', :text => 'Add a new Draft')
should have_no_selector('a', :text => 'Add a new Comment')
end

it "can show the add button on an associated field" do
RailsAdmin.config Player do
edit do
field :team do
inline_add true
end
field :draft do
inline_add true
end
field :comments do
inline_add true
end
end
end
visit new_path(:model_name => "player")
should have_selector('a', :text => 'Add a new Team')
should have_selector('a', :text => 'Add a new Draft')
should have_selector('a', :text => 'Add a new Comment')
end

it "can hide the edit button on an associated field" do
RailsAdmin.config Player do
edit do
field :team do
inline_edit false
end
field :draft do
inline_edit false
end
end
end
visit new_path(:model_name => "player")
should have_no_selector('a', :text => 'Edit this Team')
should have_no_selector('a', :text => 'Edit this Draft')
end

it "can show the edit button on an associated field" do
RailsAdmin.config Player do
edit do
field :team do
inline_edit true
end
field :draft do
inline_edit true
end
end
end
visit new_path(:model_name => "player")
should have_selector('a', :text => 'Edit this Team')
should have_selector('a', :text => 'Edit this Draft')
end

end

describe "bindings" do
Expand Down Expand Up @@ -1023,4 +1096,5 @@ def color_enum
should have_selector(".color_type input")
end
end

end

0 comments on commit 8533e5a

Please sign in to comment.