Skip to content

Commit

Permalink
read only association are not editable anymore.
Browse files Browse the repository at this point in the history
  • Loading branch information
bbenezech committed Aug 2, 2011
1 parent 4a8c98d commit df0f7b8
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 5 deletions.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -1207,6 +1207,8 @@ You can edit related objects in filtering-multiselect by double-clicking on any
If you set the :inverse_of option on your relations, RailsAdmin will automatically populate the inverse relationship
in the modal creation window. (link next to belongs\_to and has\_many widgets)

:readonly options are automatically inferred on associations fields and won't be editable in forms.

### Configuring fields ###

* exclude_fields field_list
Expand Down
7 changes: 6 additions & 1 deletion lib/rails_admin/adapters/active_record.rb
Expand Up @@ -137,7 +137,8 @@ def associations
:foreign_type => association_foreign_type_lookup(association),
:as => association_as_lookup(association),
:polymorphic => association_polymorphic_lookup(association),
:inverse_of => association_inverse_of_lookup(association)
:inverse_of => association_inverse_of_lookup(association),
:read_only => association_read_only_lookup(association)
}
end
end
Expand Down Expand Up @@ -209,6 +210,10 @@ def association_parent_key_lookup(association)
def association_inverse_of_lookup(association)
association.options[:inverse_of].try :to_sym
end

def association_read_only_lookup(association)
association.options[:readonly]
end

def association_child_model_lookup(association)
case association.macro
Expand Down
6 changes: 5 additions & 1 deletion lib/rails_admin/config/fields.rb
Expand Up @@ -10,7 +10,11 @@ module Fields
# If it's an association
if properties.has_key?(:parent_model)
association = parent.abstract_model.associations.find {|a| a[:name].to_s == properties[:name].to_s}
fields << RailsAdmin::Config::Fields::Types.load("#{association[:polymorphic] ? :polymorphic : properties[:type]}_association").new(parent, properties[:name], association)
field = RailsAdmin::Config::Fields::Types.load("#{association[:polymorphic] ? :polymorphic : properties[:type]}_association").new(parent, properties[:name], association)

field.read_only(true) if association[:read_only]
fields << field

# If it's a column
elsif !properties.has_key?(:parent_model)
fields << (field = RailsAdmin::Config::Fields::Types.load(properties[:type]).new(parent, properties[:name], properties))
Expand Down
2 changes: 1 addition & 1 deletion lib/rails_admin/config/fields/association.rb
Expand Up @@ -79,7 +79,7 @@ def child_key
def inverse_of
association[:inverse_of]
end

# Reader for validation errors of the bound object
def errors
bindings[:object].errors[child_key]
Expand Down
2 changes: 1 addition & 1 deletion spec/dummy_app/app/models/league.rb
Expand Up @@ -2,7 +2,7 @@ class League < ActiveRecord::Base
validates_presence_of(:name)

has_many :divisions
has_many :teams, :through => :divisions
has_many :teams, :through => :divisions, :readonly => true

def custom_name
"League '#{self.name}'"
Expand Down
12 changes: 11 additions & 1 deletion spec/requests/basic/edit/rails_admin_basic_edit_spec.rb
Expand Up @@ -39,7 +39,17 @@
should have_selector("a", :href => 'admin/players/new?associations[team]=' + @team.id.to_s)
end
end


describe "readonly associations" do

it 'should not be editable' do
@league = FactoryGirl.create :league
visit rails_admin_edit_path(:model_name => "league", :id => @league.id)
should_not have_selector('select#league_team_ids')
should have_selector('select#league_division_ids') # decoy, fails if naming scheme changes
end
end

describe "edit with has-one association" do
before(:each) do
@player = FactoryGirl.create :player
Expand Down

0 comments on commit df0f7b8

Please sign in to comment.