Skip to content
This repository has been archived by the owner on Dec 12, 2021. It is now read-only.

Commit

Permalink
disabling attr_protected
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanb committed Jun 1, 2009
1 parent 525c3f2 commit e9c0c6f
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/trusted_params.rb
@@ -1,2 +1,3 @@
$:.unshift(File.dirname(__FILE__))
require 'trusted_params/hash_additions'
require 'trusted_params/active_record_additions'
18 changes: 18 additions & 0 deletions lib/trusted_params/active_record_additions.rb
@@ -0,0 +1,18 @@
module TrustedParams
module ActiveRecordAdditions
def self.included(base)
base.extend(ClassMethods)
end

module ClassMethods
def attr_protected(*args)
raise "attr_protected has been disabled by trusted-params plugin, use attr_accessible"
end
end
end
end

# TODO for some reason this doesn't work for overriding methods
# ActiveRecord::Base.class_eval do
# include TrustedParams::ActiveRecordAdditions
# end
41 changes: 41 additions & 0 deletions spec/spec_helper.rb
Expand Up @@ -7,3 +7,44 @@
Spec::Runner.configure do |config|
config.mock_with :rr
end

class MockedModel < ActiveRecord::Base

include TrustedParams::ActiveRecordAdditions

class_inheritable_hash :paginate_options

def self.paginate(options)
self.paginate_options = options
end

def self.add_column(name, column_type = :string)
returning ActiveRecord::ConnectionAdapters::Column.new(name, nil) do |column|
def column.type
column_type
end
@columns ||= []
@columns << column
end
end

def self.reset_columns
@columns = []
end

def self.columns
@columns || []
end

def self.columns_hash
columns.index_by{|c| c.name.to_s}
end

def self.inspect
"Model Mock"
end

def self.table_name
'mocked_models'
end
end
12 changes: 12 additions & 0 deletions spec/trusted_params/active_record_additions_spec.rb
@@ -0,0 +1,12 @@
require File.dirname(__FILE__) + '/../spec_helper'

describe MockedModel do
before(:each) do
MockedModel.reset_columns
MockedModel.add_column(:name)
end

it "should not allow one to set attr_protected" do
lambda { MockedModel.attr_protected(:foo) }.should raise_error
end
end

0 comments on commit e9c0c6f

Please sign in to comment.