safe_attributes 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem 'activesupport', '~> 3.0.0'
4
+ gem 'activerecord', '~> 3.0.0'
5
+
6
+ group :development do
7
+ gem 'jeweler', '>=1.5.2'
8
+ gem 'rake', '>=0.8.7'
9
+ gem 'rspec', '>= 2.3.0'
10
+ gem 'rcov'
11
+ gem 'sqlite3-ruby'
12
+ end
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 C. Brian Jones
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,81 @@
1
+ = SafeAttributes
2
+
3
+ By default Rails creates attribute accessors for all database table columns
4
+ in each model. Columns with specific names cause errors because they result
5
+ in Rails redefining a key method within either Ruby or Rails in an
6
+ incompatible way. A classic example for me has been tables with a column
7
+ named 'class', though there are many other possible examples.
8
+
9
+ Using this gem enhances activerecord to change the default behavior for
10
+ the creation of attribute accessors. Methods in ActiveRecord::Base and
11
+ its superclasses, except for 'id', are combined into a list. This list
12
+ is checked before the creation of two types of attribute accessors:
13
+ attribute() and attribute=().
14
+
15
+ You can add to this list by calling bad_attribute_names with a list of
16
+ method names you do not want generated. Rails generates additional methods
17
+ that this module does not prevent the creation of:
18
+
19
+ * attribute_before_type_cast
20
+ * attribute_changed?
21
+ * attribute_was
22
+ * attribute_will_change!
23
+ * attribute?
24
+
25
+ These largely should not run afoul of Ruby or ActiveRecord in most cases.
26
+
27
+ To access an attribute in ActiveRecord without its normal getter or setter
28
+ you can use a couple of different approaches.
29
+
30
+ * model_instance[:attribute] # works as both getter and setter like a hash
31
+ * model_instance.read_attribute('attribute')
32
+ * model_instance.write_attribute('attribute', value)
33
+
34
+ == Installing
35
+
36
+ gem install safe_attributes --force
37
+
38
+ The --force flag is here because I believe rubygems to have some sort of
39
+ dependency tracking issue. Without it, it wants to install the newest
40
+ activemodel (3.0.3 at this time) and arel. These are not necessary
41
+ for the gem and this should not be happening. As long as you have
42
+ activerecord 3.0.0 or better it should work. Here's the error message
43
+ in case you see it.
44
+
45
+ ERROR: Error installing safe_attributes:
46
+ activemodel requires activesupport (= 3.0.3, runtime)
47
+
48
+ == Using
49
+
50
+ === Rails
51
+ Add safe_attributes to your Gemfile.
52
+
53
+ gem 'safe_attributes'
54
+
55
+ SafeAttributes is included into ActiveRecord::Base automatically. While
56
+ nothing else should be necessary, you can still add to the list of bad
57
+ attributes if you find it necessary.
58
+
59
+ class MyModel < ActiveRecord::Base
60
+ bad_attribute_names :my_attr
61
+ end
62
+
63
+ === Outside of Rails
64
+ require 'safe_attributes'
65
+ class MyModel < ActiveRecord::Base
66
+ include SafeAttributes
67
+ end
68
+
69
+ == Note on Patches/Pull Requests
70
+
71
+ * Fork the project.
72
+ * Make your feature addition or bug fix.
73
+ * Add tests for it. This is important so I don't break it in a
74
+ future version unintentionally.
75
+ * Commit, do not mess with rakefile, version, or history.
76
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
77
+ * Send me a pull request. Bonus points for topic branches.
78
+
79
+ == Copyright
80
+
81
+ Copyright (c) 2010 C. Brian Jones. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,44 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ gem.name = "safe_attributes"
17
+ gem.version = version
18
+ gem.homepage = "http://github.com/bjones/safe_attributes"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Add support for reserved word column names with ActiveRecord}
21
+ gem.description = %Q{If your schema has columns named type, or class, or any other name that conflicts with a method of ActiveRecord or one of its superclasses, you will need this gem to use Rails 3 with that database.}
22
+ gem.email = "cbj@gnu.org"
23
+ gem.authors = ["Brian Jones"]
24
+ end
25
+ Jeweler::RubygemsDotOrgTasks.new
26
+
27
+ require 'rspec/core'
28
+ require 'rspec/core/rake_task'
29
+ RSpec::Core::RakeTask.new(:spec) do |spec|
30
+ end
31
+
32
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
33
+ spec.rcov = true
34
+ end
35
+
36
+ task :default => :spec
37
+
38
+ require 'rake/rdoctask'
39
+ Rake::RDocTask.new do |rdoc|
40
+ rdoc.rdoc_dir = 'rdoc'
41
+ rdoc.title = "SafeAttributes #{version}"
42
+ rdoc.rdoc_files.include('README*')
43
+ rdoc.rdoc_files.include('lib/**/*.rb')
44
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.1
@@ -0,0 +1,47 @@
1
+ module SafeAttributes
2
+ extend ActiveSupport::Concern
3
+
4
+ module ClassMethods
5
+ #
6
+ # Within your model call this method once with a list of
7
+ # methods matching either attribute() or attribute=() for
8
+ # attributes (column names) you do not want to create the
9
+ # the normal method for. You should not need to do this
10
+ # but the option is there in case the default list is
11
+ # inadequate
12
+ #
13
+ def bad_attribute_names(*attrs)
14
+ @bad_attribute_names ||= lambda {
15
+ methods = Array.new(attrs.collect { |x| x.to_s })
16
+ methods += ActiveRecord::Base.public_instance_methods
17
+ methods += ActiveRecord::Base.protected_instance_methods
18
+ methods += ActiveRecord::Base.private_instance_methods
19
+ methods -= ['id']
20
+ return methods
21
+ }.call
22
+ end
23
+
24
+ #
25
+ # Override the default implementation to not create the
26
+ # attribute() method if that method name is in the list of
27
+ # bad names
28
+ #
29
+ def define_method_attribute(attr_name)
30
+ return if (bad_attribute_names.include?(attr_name))
31
+ super(attr_name)
32
+ end
33
+
34
+ #
35
+ # Override the default implementation to not create the
36
+ # attribute= method if that method name is in the list of
37
+ # bad names
38
+ #
39
+ def define_method_attribute=(attr_name)
40
+ method = attr_name + '='
41
+ return if (bad_attribute_names.include?(method))
42
+ super(attr_name)
43
+ end
44
+ end
45
+ end
46
+
47
+ require 'safe_attributes/railtie.rb' if defined?(Rails)
@@ -0,0 +1,12 @@
1
+ require 'safe_attributes'
2
+ require 'rails'
3
+
4
+ module SafeAttributes
5
+ class Railtie < ::Rails::Railtie
6
+ initializer "safeattributes.active_record" do |app|
7
+ ActiveSupport.on_load :active_record do
8
+ ActiveRecord::Base.send :include, SafeAttributes
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,74 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{safe_attributes}
8
+ s.version = "1.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Brian Jones"]
12
+ s.date = %q{2010-12-25}
13
+ s.description = %q{If your schema has columns named type, or class, or any other name that conflicts with a method of ActiveRecord or one of its superclasses, you will need this gem to use Rails 3 with that database.}
14
+ s.email = %q{cbj@gnu.org}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".rspec",
22
+ "Gemfile",
23
+ "LICENSE",
24
+ "README.rdoc",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "lib/safe_attributes.rb",
28
+ "lib/safe_attributes/railtie.rb",
29
+ "safe_attributes.gemspec",
30
+ "spec/safe_attributes/safe_attributes_spec.rb",
31
+ "spec/spec_helper.rb"
32
+ ]
33
+ s.homepage = %q{http://github.com/bjones/safe_attributes}
34
+ s.licenses = ["MIT"]
35
+ s.require_paths = ["lib"]
36
+ s.rubygems_version = %q{1.3.7}
37
+ s.summary = %q{Add support for reserved word column names with ActiveRecord}
38
+ s.test_files = [
39
+ "spec/safe_attributes/safe_attributes_spec.rb",
40
+ "spec/spec_helper.rb"
41
+ ]
42
+
43
+ if s.respond_to? :specification_version then
44
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
45
+ s.specification_version = 3
46
+
47
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
48
+ s.add_runtime_dependency(%q<activesupport>, ["~> 3.0.0"])
49
+ s.add_runtime_dependency(%q<activerecord>, ["~> 3.0.0"])
50
+ s.add_development_dependency(%q<jeweler>, [">= 1.5.2"])
51
+ s.add_development_dependency(%q<rake>, [">= 0.8.7"])
52
+ s.add_development_dependency(%q<rspec>, [">= 2.3.0"])
53
+ s.add_development_dependency(%q<rcov>, [">= 0"])
54
+ s.add_development_dependency(%q<sqlite3-ruby>, [">= 0"])
55
+ else
56
+ s.add_dependency(%q<activesupport>, ["~> 3.0.0"])
57
+ s.add_dependency(%q<activerecord>, ["~> 3.0.0"])
58
+ s.add_dependency(%q<jeweler>, [">= 1.5.2"])
59
+ s.add_dependency(%q<rake>, [">= 0.8.7"])
60
+ s.add_dependency(%q<rspec>, [">= 2.3.0"])
61
+ s.add_dependency(%q<rcov>, [">= 0"])
62
+ s.add_dependency(%q<sqlite3-ruby>, [">= 0"])
63
+ end
64
+ else
65
+ s.add_dependency(%q<activesupport>, ["~> 3.0.0"])
66
+ s.add_dependency(%q<activerecord>, ["~> 3.0.0"])
67
+ s.add_dependency(%q<jeweler>, [">= 1.5.2"])
68
+ s.add_dependency(%q<rake>, [">= 0.8.7"])
69
+ s.add_dependency(%q<rspec>, [">= 2.3.0"])
70
+ s.add_dependency(%q<rcov>, [">= 0"])
71
+ s.add_dependency(%q<sqlite3-ruby>, [">= 0"])
72
+ end
73
+ end
74
+
@@ -0,0 +1,92 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ # create the table for the test in the database
4
+ ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS 'my_models'")
5
+ ActiveRecord::Base.connection.create_table(:my_models) do |t|
6
+ t.string :class
7
+ t.string :bad_attribute
8
+ t.string :good_attribute
9
+ end
10
+
11
+ class MyModel < ActiveRecord::Base
12
+ include SafeAttributes
13
+ bad_attribute_names :bad_attribute, :bad_attribute=
14
+ end
15
+
16
+ describe MyModel do
17
+
18
+ before(:each) do
19
+ ActiveRecord::Base.connection.increment_open_transactions
20
+ ActiveRecord::Base.connection.begin_db_transaction
21
+ @model = MyModel.new
22
+ end
23
+
24
+ after(:each) do
25
+ ActiveRecord::Base.connection.rollback_db_transaction
26
+ ActiveRecord::Base.connection.decrement_open_transactions
27
+ end
28
+
29
+ it "inspecting class returns expected attribute names" do
30
+ MyModel.inspect.should match 'class: string'
31
+ end
32
+
33
+ it "inspecting instance returns expected attribute names" do
34
+ @model.inspect.should match 'class: nil'
35
+ end
36
+
37
+ it "does not redefine class()" do
38
+ @model.class.name.should == 'MyModel'
39
+ end
40
+
41
+ it "defines class=()" do
42
+ @model.respond_to?('class=') # to force method generation
43
+ @model.methods.include?('class=').should be_true
44
+ end
45
+
46
+ it "does not define bad_attribute()" do
47
+ @model.respond_to?('bad_attribute') # to force method generation
48
+ @model.methods.include?('bad_attribute').should be_false
49
+ end
50
+
51
+ it "does not define bad_attribute=()" do
52
+ @model.respond_to?('bad_attribute=') # to force method generation
53
+ @model.methods.include?('bad_attribute=').should be_false
54
+ end
55
+
56
+ it "does define good_attribute()" do
57
+ @model.respond_to?('good_attribute') # to force method generation
58
+ @model.methods.include?('good_attribute').should be_true
59
+ end
60
+
61
+ it "does define good_attribute=()" do
62
+ @model.respond_to?('good_attribute=') # to force method generation
63
+ @model.methods.include?('good_attribute=').should be_true
64
+ end
65
+
66
+ it "does define id()" do
67
+ @model.respond_to?('id') # to force method generation
68
+ @model.methods.include?('id').should be_true
69
+ end
70
+
71
+ it "can create instance in database with special attribute name" do
72
+ m = MyModel.create(:class => 'Foo')
73
+ m = MyModel.find(m.id)
74
+ m[:class].should == 'Foo'
75
+ end
76
+
77
+ it "has class attribute" do
78
+ MyModel.new().has_attribute?('class').should be_true
79
+ end
80
+
81
+ it "can call class= without error" do
82
+ m = MyModel.new()
83
+ m.class = 'Foo'
84
+ m[:class].should == 'Foo'
85
+ end
86
+
87
+ it "can use finders with attribute" do
88
+ m = MyModel.find_all_by_class('Foo')
89
+ m.size.should == 0
90
+ end
91
+ end
92
+
@@ -0,0 +1,22 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+
4
+ require 'rubygems'
5
+ gem 'activerecord', '>= 3.0.0'
6
+ require 'active_record'
7
+ gem 'activesupport', '>= 3.0.0'
8
+ require 'active_support'
9
+
10
+ require 'safe_attributes'
11
+ require 'rspec'
12
+ require 'rspec/autorun'
13
+
14
+ root = File.expand_path(File.join(File.dirname(__FILE__), '..'))
15
+ ActiveRecord::Base.establish_connection(
16
+ :adapter => "sqlite3",
17
+ :database => "#{root}/db/safeattributes.db"
18
+ )
19
+
20
+ RSpec.configure do |config|
21
+ end
22
+
metadata ADDED
@@ -0,0 +1,187 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: safe_attributes
3
+ version: !ruby/object:Gem::Version
4
+ hash: 21
5
+ prerelease: false
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 1
10
+ version: 1.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Brian Jones
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-12-25 00:00:00 -05:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ type: :runtime
23
+ prerelease: false
24
+ name: activesupport
25
+ version_requirements: &id001 !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ~>
29
+ - !ruby/object:Gem::Version
30
+ hash: 7
31
+ segments:
32
+ - 3
33
+ - 0
34
+ - 0
35
+ version: 3.0.0
36
+ requirement: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ type: :runtime
39
+ prerelease: false
40
+ name: activerecord
41
+ version_requirements: &id002 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ~>
45
+ - !ruby/object:Gem::Version
46
+ hash: 7
47
+ segments:
48
+ - 3
49
+ - 0
50
+ - 0
51
+ version: 3.0.0
52
+ requirement: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ type: :development
55
+ prerelease: false
56
+ name: jeweler
57
+ version_requirements: &id003 !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ hash: 7
63
+ segments:
64
+ - 1
65
+ - 5
66
+ - 2
67
+ version: 1.5.2
68
+ requirement: *id003
69
+ - !ruby/object:Gem::Dependency
70
+ type: :development
71
+ prerelease: false
72
+ name: rake
73
+ version_requirements: &id004 !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ hash: 49
79
+ segments:
80
+ - 0
81
+ - 8
82
+ - 7
83
+ version: 0.8.7
84
+ requirement: *id004
85
+ - !ruby/object:Gem::Dependency
86
+ type: :development
87
+ prerelease: false
88
+ name: rspec
89
+ version_requirements: &id005 !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ hash: 3
95
+ segments:
96
+ - 2
97
+ - 3
98
+ - 0
99
+ version: 2.3.0
100
+ requirement: *id005
101
+ - !ruby/object:Gem::Dependency
102
+ type: :development
103
+ prerelease: false
104
+ name: rcov
105
+ version_requirements: &id006 !ruby/object:Gem::Requirement
106
+ none: false
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ hash: 3
111
+ segments:
112
+ - 0
113
+ version: "0"
114
+ requirement: *id006
115
+ - !ruby/object:Gem::Dependency
116
+ type: :development
117
+ prerelease: false
118
+ name: sqlite3-ruby
119
+ version_requirements: &id007 !ruby/object:Gem::Requirement
120
+ none: false
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ hash: 3
125
+ segments:
126
+ - 0
127
+ version: "0"
128
+ requirement: *id007
129
+ description: If your schema has columns named type, or class, or any other name that conflicts with a method of ActiveRecord or one of its superclasses, you will need this gem to use Rails 3 with that database.
130
+ email: cbj@gnu.org
131
+ executables: []
132
+
133
+ extensions: []
134
+
135
+ extra_rdoc_files:
136
+ - LICENSE
137
+ - README.rdoc
138
+ files:
139
+ - .document
140
+ - .rspec
141
+ - Gemfile
142
+ - LICENSE
143
+ - README.rdoc
144
+ - Rakefile
145
+ - VERSION
146
+ - lib/safe_attributes.rb
147
+ - lib/safe_attributes/railtie.rb
148
+ - safe_attributes.gemspec
149
+ - spec/safe_attributes/safe_attributes_spec.rb
150
+ - spec/spec_helper.rb
151
+ has_rdoc: true
152
+ homepage: http://github.com/bjones/safe_attributes
153
+ licenses:
154
+ - MIT
155
+ post_install_message:
156
+ rdoc_options: []
157
+
158
+ require_paths:
159
+ - lib
160
+ required_ruby_version: !ruby/object:Gem::Requirement
161
+ none: false
162
+ requirements:
163
+ - - ">="
164
+ - !ruby/object:Gem::Version
165
+ hash: 3
166
+ segments:
167
+ - 0
168
+ version: "0"
169
+ required_rubygems_version: !ruby/object:Gem::Requirement
170
+ none: false
171
+ requirements:
172
+ - - ">="
173
+ - !ruby/object:Gem::Version
174
+ hash: 3
175
+ segments:
176
+ - 0
177
+ version: "0"
178
+ requirements: []
179
+
180
+ rubyforge_project:
181
+ rubygems_version: 1.3.7
182
+ signing_key:
183
+ specification_version: 3
184
+ summary: Add support for reserved word column names with ActiveRecord
185
+ test_files:
186
+ - spec/safe_attributes/safe_attributes_spec.rb
187
+ - spec/spec_helper.rb