Skip to content

Commit

Permalink
fix railsadminteam#1321 add a warning message when a field is not edi…
Browse files Browse the repository at this point in the history
…table because of an ActiveModel attr_accessible restriction
  • Loading branch information
bbenezech committed Sep 13, 2012
1 parent 4879448 commit 6ac9dd7
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
4 changes: 4 additions & 0 deletions lib/rails_admin/config.rb
Expand Up @@ -69,6 +69,9 @@ class << self
# accepts a hash of static links to be shown below the main navigation
attr_accessor :navigation_static_links
attr_accessor :navigation_static_label

# yell about fields that are not marked as accessible
attr_accessor :yell_for_non_accessible_fields

# Setup authentication to be run as a before filter
# This is run inside the controller instance so you can setup any authentication you need to
Expand Down Expand Up @@ -280,6 +283,7 @@ def models(&block)
# @see RailsAdmin::Config.registry
def reset
@compact_show_view = true
@yell_for_non_accessible_fields = true
@authenticate = nil
@authorize = nil
@audit = nil
Expand Down
7 changes: 6 additions & 1 deletion lib/rails_admin/config/fields/base.rb
Expand Up @@ -213,7 +213,12 @@ def virtual?

def editable?
return false if @properties && @properties[:read_only]
!bindings[:object].class.active_authorizer[bindings[:view].controller.send(:_attr_accessible_role)].deny?(self.method_name)
active_model_attr_accessible = !bindings[:object].class.active_authorizer[bindings[:view].controller.send(:_attr_accessible_role)].deny?(self.method_name)
return true if active_model_attr_accessible
if RailsAdmin::Config.yell_for_non_accessible_fields
Rails.logger.debug "\n\n[RailsAdmin] Please add 'attr_accessible :#{self.method_name}' in your '#{bindings[:object].class}' model definition if you want to make it editable.\nYou can also explicitely mark this field as read-only: \n\nconfig.model #{bindings[:object].class} do\n field :#{self.name} do\n read_only true\n end\nend\n\nAdd 'config.yell_for_non_accessible_fields = false' in your 'rails_admin.rb' initializer if you do not want to see these warnings\n\n"
end
false
end

# Is this an association
Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Expand Up @@ -58,6 +58,7 @@ def password_digest(password)
RailsAdmin::Config.reset
RailsAdmin::AbstractModel.reset
RailsAdmin::Config.audit_with(:history) if CI_ORM == :active_record
RailsAdmin::Config.yell_for_non_accessible_fields = false
login_as User.create(
:email => "username@example.com",
:password => "password"
Expand Down
26 changes: 26 additions & 0 deletions spec/unit/config/fields/base_spec.rb
Expand Up @@ -360,4 +360,30 @@ class FieldVisibilityTest < Tableless
RailsAdmin.config(FieldVisibilityTest).show.fields.select{|f| f.visible? }.map(&:name).should =~ [:name]
end
end

describe '#editable?' do
it 'should yell for non attr_accessible fields if config.yell_for_non_accessible_fields is true' do
RailsAdmin.config do |config|
config.yell_for_non_accessible_fields = true
config.model FieldTest do
field :protected_field
end
end
Rails.logger.should_receive(:debug).with {|msg| msg =~ /Please add 'attr_accessible :protected_field'/ }
editable = RailsAdmin.config(FieldTest).field(:protected_field).with(:object => FactoryGirl.create(:field_test), :view => double(:controller => double(:_attr_accessible_role => :default))).editable?
editable.should == false
end

it 'should not yell for non attr_accessible fields if config.yell_for_non_accessible_fields is false' do
RailsAdmin.config do |config|
config.yell_for_non_accessible_fields = false
config.model FieldTest do
field :protected_field
end
end
Rails.logger.should_not_receive(:debug).with {|msg| msg =~ /Please add 'attr_accessible :protected_field'/ }
editable = RailsAdmin.config(FieldTest).field(:protected_field).with(:object => FactoryGirl.create(:field_test), :view => double(:controller => double(:_attr_accessible_role => :default))).editable?
editable.should == false
end
end
end

0 comments on commit 6ac9dd7

Please sign in to comment.