Skip to content

Commit 1c5b3bf

Browse files
committed
Disable attr_accessible for rails >= 4.0.
1 parent 848bee8 commit 1c5b3bf

File tree

3 files changed

+42
-14
lines changed

3 files changed

+42
-14
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ garlic
33
nbproject
44
rdoc
55
gemfiles/*.lock
6+
log/*

test/model_test.rb

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ class TestModel < ActiveRecord::Base
2626
:class_name => "TestAttrThrough", :source => :test_attr_throughs,
2727
:conditions => "test_attrs.attr = 1"
2828

29-
attr_accessible :content, :test_attr_through_id, :country_id
29+
if Rails.version < "4"
30+
attr_accessible :content, :test_attr_through_id, :country_id
31+
end
3032

3133
# TODO currently not working in Rails 3
3234
if Rails.version < "3"
@@ -75,9 +77,11 @@ class TestAttr < ActiveRecord::Base
7577
has_many :test_attr_throughs
7678
has_many :test_model_security_model_with_finds
7779
attr_reader :role_symbols
78-
attr_accessible :test_model, :test_another_model, :attr, :branch, :company, :test_attr,
79-
:test_a_third_model, :n_way_join_item, :n_way_join_item_id, :test_attr_through_id,
80-
:test_model_id, :test_another_model_id
80+
if Rails.version < "4"
81+
attr_accessible :test_model, :test_another_model, :attr, :branch, :company, :test_attr,
82+
:test_a_third_model, :n_way_join_item, :n_way_join_item_id, :test_attr_through_id,
83+
:test_model_id, :test_another_model_id
84+
end
8185
def initialize (*args)
8286
@role_symbols = []
8387
super(*args)
@@ -91,7 +95,9 @@ class TestAttrThrough < ActiveRecord::Base
9195
class TestModelSecurityModel < ActiveRecord::Base
9296
has_many :test_attrs
9397
using_access_control
94-
attr_accessible :attr, :attr_2, :test_attrs
98+
if Rails.version < "4"
99+
attr_accessible :attr, :attr_2, :test_attrs
100+
end
95101
end
96102
class TestModelSecurityModelWithFind < ActiveRecord::Base
97103
if Rails.version < "3.2"
@@ -103,19 +109,25 @@ class TestModelSecurityModelWithFind < ActiveRecord::Base
103109
belongs_to :test_attr
104110
using_access_control :include_read => true,
105111
:context => :test_model_security_models
106-
attr_accessible :test_attr, :attr
112+
if Rails.version < "4"
113+
attr_accessible :test_attr, :attr
114+
end
107115
end
108116

109117
class Branch < ActiveRecord::Base
110118
has_many :test_attrs
111119
belongs_to :company
112-
attr_accessible :name, :company
120+
if Rails.version < "4"
121+
attr_accessible :name, :company
122+
end
113123
end
114124
class Company < ActiveRecord::Base
115125
has_many :test_attrs
116126
has_many :branches
117127
belongs_to :country
118-
attr_accessible :name, :country, :country_id
128+
if Rails.version < "4"
129+
attr_accessible :name, :country, :country_id
130+
end
119131
end
120132
class SmallCompany < Company
121133
def self.decl_auth_context
@@ -125,7 +137,9 @@ def self.decl_auth_context
125137
class Country < ActiveRecord::Base
126138
has_many :test_models
127139
has_many :companies
128-
attr_accessible :name
140+
if Rails.version < "4"
141+
attr_accessible :name
142+
end
129143
end
130144

131145
class NamedScopeModelTest < Test::Unit::TestCase
@@ -781,7 +795,13 @@ def test_with_contains
781795
user = MockUser.new(:test_role,
782796
:id => test_model_1.test_attrs.first.id)
783797
assert_equal 1, TestModel.with_permissions_to(:read, :user => user).length
784-
assert_equal 1, TestModel.with_permissions_to(:read, :user => user).find(:all, :conditions => {:id => test_model_1.id}).length
798+
if Rails.version < "4"
799+
assert_equal 1, TestModel.with_permissions_to(:read, :user => user).
800+
find(:all, :conditions => {:id => test_model_1.id}).length
801+
else
802+
assert_equal 1, TestModel.with_permissions_to(:read, :user => user).
803+
where( :id => test_model_1.id ).length
804+
end
785805

786806
TestModel.delete_all
787807
TestAttr.delete_all

test/test_helper.rb

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ def warn?; end
114114
else
115115
class TestApp
116116
class Application < ::Rails::Application
117+
config.secret_key_base = "testingpurposesonly"
117118
config.active_support.deprecation = :stderr
118119
database_path = File.expand_path('../database.yml', __FILE__)
119120
if Rails.version.start_with? '3.0.'
@@ -128,9 +129,11 @@ class ApplicationController < ActionController::Base
128129
end
129130
#Rails::Application.routes.draw do
130131
Rails.application.routes.draw do
131-
match '/name/spaced_things(/:action)' => 'name/spaced_things'
132-
match '/deep/name_spaced/things(/:action)' => 'deep/name_spaced/things'
133-
match '/:controller(/:action(/:id))'
132+
match '/name/spaced_things(/:action)' => 'name/spaced_things',
133+
:via => [ :get, :post ]
134+
match '/deep/name_spaced/things(/:action)' => 'deep/name_spaced/things',
135+
:via => [ :get, :post ]
136+
match '/:controller(/:action(/:id))', :via => [ :get, :post ]
134137
end
135138
end
136139

@@ -141,8 +144,12 @@ class ApplicationController < ActionController::Base
141144

142145
class Test::Unit::TestCase
143146
include Authorization::TestHelper
147+
end
144148

145-
def request! (user, action, reader, params = {})
149+
class ActionController::TestCase
150+
include Authorization::TestHelper
151+
152+
def request!(user, action, reader, params = {})
146153
action = action.to_sym if action.is_a?(String)
147154
@controller.current_user = user
148155
@controller.authorization_engine = Authorization::Engine.new(reader)

0 commit comments

Comments
 (0)