Permalink
Browse files

Merge branch '0.5-series'

Conflicts:
	CHANGELOG.md
	lib/active_attr/version.rb
  • Loading branch information...
2 parents f3d324f + 4280c09 commit ac6cf0de65a49b1ab317dac2ecb1256725909ea9 @cgriego committed Mar 16, 2012
View
@@ -6,6 +6,7 @@ rvm:
bundler_args: --without development
gemfile:
- Gemfile
+ - gemfiles/rails_3_0.gemfile
- gemfiles/rails_3_1.gemfile
- gemfiles/rails_head.gemfile
matrix:
View
@@ -8,6 +8,10 @@
* TypecastedAttributes now supports a :typecaster option on attribute
definitions which can be any object that responds to #call
+# ActiveAttr 0.5.1 (March 16, 2012) #
+
+* ActiveAttr now supports Rails 3.0.2+ (Egor Baranov)
+
# ActiveAttr 0.5.0 (March 11, 2012) #
* ActiveAttr is now Ruby warning free
View
@@ -18,8 +18,8 @@ Gem::Specification.new do |s|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]
- s.add_runtime_dependency "activemodel", ">= 3.1", "< 4.1"
- s.add_runtime_dependency "activesupport", ">= 3.1", "< 4.1"
+ s.add_runtime_dependency "activemodel", ">= 3.0.2", "< 4.1"
+ s.add_runtime_dependency "activesupport", ">= 3.0.2", "< 4.1"
s.add_development_dependency "bundler", "~> 1.0"
s.add_development_dependency "factory_girl", "~> 2.2"
@@ -0,0 +1,6 @@
+source "http://rubygems.org"
+
+gemspec :development_group => :test, :path => ".."
+
+gem "activemodel", "~> 3.0.2"
+gem "activesupport", "~> 3.0.2"
@@ -1,6 +1,3 @@
-require 'active_support/core_ext/hash/reverse_merge'
-require 'active_support/core_ext/string/inflections'
-
module ActiveAttr
# Represents an attribute for reflection
#
@@ -165,7 +165,9 @@ module ClassMethods
def attribute(name, options={})
AttributeDefinition.new(name, options).tap do |attribute_definition|
attribute_name = attribute_definition.name.to_s
- define_attribute_method attribute_definition.name unless attribute_names.include? attribute_name
+ # Force active model to generate attribute methods
+ remove_instance_variable("@attribute_methods_generated") if instance_variable_defined?("@attribute_methods_generated")
+ define_attribute_methods([attribute_definition.name]) unless attribute_names.include? attribute_name
attributes[attribute_name] = attribute_definition
end
end
@@ -32,8 +32,13 @@ module MassAssignmentSecurity
# @since 0.3.0
def assign_attributes(new_attributes, options={})
if new_attributes && !options[:without_protection]
- mass_assignment_role = options[:as] || :default
- new_attributes = sanitize_for_mass_assignment new_attributes, mass_assignment_role
+ if method(:sanitize_for_mass_assignment).arity.abs > 1
+ mass_assignment_role = options[:as] || :default
+ new_attributes = sanitize_for_mass_assignment new_attributes, mass_assignment_role
+ else
+ # Rails 3.0 has no roles support in mass assignment
+ new_attributes = sanitize_for_mass_assignment new_attributes
+ end
end
super
@@ -25,7 +25,9 @@ class BigDecimalTypecaster
#
# @since 0.5.0
def call(value)
- if value.respond_to? :to_d
+ if value.is_a? BigDecimal
+ value
+ elsif value.respond_to? :to_d
value.to_d
else
BigDecimal.new value.to_s
@@ -9,8 +9,10 @@ module ActiveAttr
subject { model_class.new }
let :model_class do
- Class.new do
- include ActiveAttr::AttributeDefaults
+ Class.new.tap do |model_class|
+ model_class.class_eval do
+ include ActiveAttr::AttributeDefaults
+ end
end
end
@@ -5,6 +5,31 @@
module ActiveAttr
describe Attributes do
+ context "defining multiple attributes" do
+ let :model_class do
+ Class.new do
+ include Attributes
+
+ attribute :name
+ attribute :id
+
+ def id
+ if defined?(super)
+ super
+ else
+ object_id
+ end
+ end unless instance_methods(false).include?("id")
+ end
+ end
+
+ subject { model_class.new }
+
+ it "correctly defines methods for the attributes instead of relying on method_missing" do
+ subject.id.should be_nil
+ end
+ end
+
context "subclassing a model" do
let :parent_class do
Class.new do
@@ -17,7 +42,7 @@ module ActiveAttr
let! :child_class do
Class.new(parent_class).tap do |child_class|
- child_class.instance_eval do
+ child_class.class_eval do
attribute :child
attribute :redefined, :type => String
end
@@ -171,7 +196,7 @@ def self.name
end
it "defining an attribute that conflicts with ActiveModel::AttributeMethods raises DangerousAttributeError" do
- expect { model_class.attribute(:attribute_method_matchers) }.to raise_error DangerousAttributeError, %{an attribute method named "attribute_method_matchers" would conflict with an existing method}
+ expect { model_class.attribute(:inspect) }.to raise_error DangerousAttributeError, %{an attribute method named "inspect" would conflict with an existing method}
end
it "defining an :id attribute does not raise" do
@@ -204,19 +229,21 @@ def self.name
end
let :dangerous_model_class do
- Class.new do
- include Attributes
+ Class.new.tap do |dangerous_model_class|
+ dangerous_model_class.class_eval do
+ include Attributes
- def method_missing(method_name, *)
- super if %w(my_proper_missing_method my_less_proper_missing_method).include? method_name.to_s
- end
+ def method_missing(method_name, *)
+ super if %w(my_proper_missing_method my_less_proper_missing_method).include? method_name.to_s
+ end
- def respond_to_missing?(method_name, *)
- method_name.to_s == "my_proper_missing_method" || super
- end
+ def respond_to_missing?(method_name, *)
+ method_name.to_s == "my_proper_missing_method" || super
+ end
- def respond_to?(method_name, include_private=false)
- super || method_name.to_s == "my_less_proper_missing_method" || (RUBY_VERSION < "1.9" && respond_to_missing?(method_name, include_private))
+ def respond_to?(method_name, include_private=false)
+ super || method_name.to_s == "my_less_proper_missing_method" || (RUBY_VERSION < "1.9" && respond_to_missing?(method_name, include_private))
+ end
end
end
end
@@ -4,6 +4,14 @@
module ActiveAttr
describe QueryAttributes do
context "defining dangerous attributes" do
+ let :parent_class do
+ Class.new.tap do |parent_class|
+ parent_class.class_eval do
+ include QueryAttributes
+ end
+ end
+ end
+
shared_examples "defining a dangerous queryable attribute" do
it "defining an attribute that conflicts with ActiveModel::AttributeMethods raises DangerousAttributeError" do
expect { model_class.attribute(:attribute_method) }.to raise_error DangerousAttributeError, %{an attribute method named "attribute_method?" would conflict with an existing method}
@@ -19,12 +27,12 @@ module ActiveAttr
end
context "on a model class" do
- let(:model_class) { Class.new { include QueryAttributes } }
+ let(:model_class) { parent_class }
include_examples "defining a dangerous queryable attribute"
end
context "on a child class" do
- let(:model_class) { Class.new(Class.new { include QueryAttributes }) }
+ let(:model_class) { Class.new(parent_class) }
include_examples "defining a dangerous queryable attribute"
end
end
View
@@ -1,5 +1,6 @@
require "bundler/setup"
require "rspec/autorun"
+require "active_model/version"
# Requires supporting files with custom matchers and macros, etc,
# in ./support/ and its subdirectories.
@@ -8,4 +9,8 @@
RSpec.configure do |config|
config.mock_framework = :rspec
config.treat_symbols_as_metadata_keys_with_true_values = true # default in RSpec 3
+
+ config.filter_run_excluding :active_model_version => lambda { |requirement|
+ !Gem::Requirement.create(requirement).satisfied_by?(Gem::Version.new(ActiveModel::VERSION::STRING))
+ }
end
@@ -26,11 +26,11 @@ module ActiveAttr
it "includes declared nil attribute defaults" do
subject.should include "age"
- subject['age'].should be_nil
+ subject["age"].should be_nil
end
it "includes declared dynamic attribute defaults" do
- subject['created_at'].should be_a_kind_of Time
+ subject["created_at"].should be_a_kind_of Time
end
end
@@ -15,14 +15,19 @@ module ActiveAttr
shared_examples "secure mass assignment method", :secure_mass_assignment_method => true do
include_examples "mass assignment method"
- it "ignores assigning a protected attribute" do
+ it "ignores assigning an attribute protected by role-based security", :active_model_version => ">= 3.1.0" do
person = mass_assign_attributes(:age => 21)
person.age.should be_nil
end
+
+ it "ignores assigning a protected attribute" do
+ person = mass_assign_attributes(:first_name => "Chris")
+ person.age.should be_nil
+ end
end
shared_examples "secure mass assignment method with options", :secure_mass_assignment_method_with_options => true do
- it "supports role-based mass assignment security" do
+ it "supports role-based mass assignment security", :active_model_version => ">= 3.1.0" do
person = mass_assign_attributes_with_options({ :age => 21 }, :as => :admin)
person.age.should == 21
end

0 comments on commit ac6cf0d

Please sign in to comment.