Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #35 from qubemove/rails-4-compat

Rails 4 compatibility
  • Loading branch information...
commit d6c437d831363b227ac498b65b890f75bcd09800 2 parents 061650c + 7b17873
@nofxx authored
View
5 Gemfile
@@ -1,8 +1,3 @@
source :rubygems
gemspec
-
-group :development do
- gem 'mongoid', '>=3.0.1'
- gem 'activerecord', '>=3.2.1'
-end
View
19 README.rdoc
@@ -11,15 +11,9 @@ and can be set using :symbols or "strings".
gem install symbolize
-=== Mongoid
-
- gem "symbolize", :require => "symbolize/mongoid"
-
-
-=== ActiveRecord
-
- gem "symbolize", :require => "symbolize/active_record"
+=== Gemfile
+ gem "symbolize"
== About
@@ -42,11 +36,20 @@ ActiveRecord:
Mongoid:
+ require 'symbolize/mongoid'
+
class Contact
include Mongoid::Symbolize
symbolize :kind, :in => [:im, :mobile, :email], :scopes => true
+or just
+
+ class Contact
+ include Symbolize::Mongoid
+
+ symbolize :kind, :in => [:im, :mobile, :email], :scopes => true
+
Other examples:
View
9 lib/symbolize.rb
@@ -1,6 +1,7 @@
module Symbolize
- # Require a specific adapter
- # symbolize/mongoid
- # symbolize/active_record
-
+ autoload :ActiveRecord, 'symbolize/active_record'
+ autoload :Mongoid, 'symbolize/mongoid'
end
+
+require 'symbolize/railtie' if defined? Rails
+
View
20 lib/symbolize/active_record.rb
@@ -1,7 +1,10 @@
+require 'active_support/concern'
+
module Symbolize
- def self.included base
- base.extend(ClassMethods)
- end
+end
+
+module Symbolize::ActiveRecord
+ extend ActiveSupport::Concern
# Symbolize ActiveRecord attributes. Add
# symbolize :attr_name
@@ -59,8 +62,6 @@ def symbolize *attr_names
default_option = configuration.delete :default
unless enum.nil?
- # Little monkeypatching, <1.8 Hashes aren't ordered.
- hsh = RUBY_VERSION > '1.9' || !defined?("ActiveSupport") ? Hash : ActiveSupport::OrderedHash
attr_names.each do |attr_name|
attr_name = attr_name.to_s
@@ -68,7 +69,7 @@ def symbolize *attr_names
if enum.is_a?(Hash)
values = enum
else
- values = hsh.new
+ values = ActiveSupport::OrderedHash.new
enum.map do |val|
key = val.respond_to?(:to_sym) ? val.to_sym : val
values[key] = capitalize ? val.to_s.capitalize : val.to_s
@@ -104,11 +105,10 @@ def symbolize *attr_names
end
if scopes
- scope_comm = lambda { |*args| ActiveRecord::VERSION::MAJOR >= 3 ? scope(*args) : named_scope(*args)}
values.each do |value|
name = value[0]
if name.respond_to?(:to_sym)
- scope_comm.call name.to_sym, :conditions => { attr_name => name.to_s }
+ scope name.to_sym, :conditions => { attr_name => name.to_s }
# Figure out if this as another option, or default...
# scope_comm.call "not_#{attr_name}".to_sym, :conditions => { attr_name != name }
end
@@ -165,7 +165,7 @@ def read_and_symbolize_attribute attr_name
def read_i18n_attribute attr_name
attr = read_attribute(attr_name)
return nil if attr.nil?
- I18n.translate("activerecord.symbolizes.#{ActiveSupport::Inflector.underscore(self.class.model_name)}.#{attr_name}.#{attr}") #.to_sym rescue nila
+ I18n.translate("activerecord.symbolizes.#{self.class.model_name.underscore}.#{attr_name}.#{attr}") #.to_sym rescue nila
end
# Write a symbolized value. Watch out for booleans.
@@ -176,5 +176,3 @@ def write_symbolized_attribute attr_name, value
self[attr_name] = val #.to_s
end
end
-
-ActiveRecord::Base.send(:include, Symbolize)
View
8 lib/symbolize/mongoid.rb
@@ -1,8 +1,8 @@
+require 'active_support/concern'
+
module Mongoid
module Symbolize
- def self.included base
- base.extend(ClassMethods)
- end
+ extend ActiveSupport::Concern
# Symbolize Mongoid attributes. Add:
# symbolize :attr_name
@@ -147,3 +147,5 @@ def symbolize *attr_names
end # ClassMethods
end # Symbolize
end # Mongoid
+
+Symbolize::Mongoid = Mongoid::Symbolize
View
20 lib/symbolize/railtie.rb
@@ -1,15 +1,11 @@
# Rails 3 initialization
module Symbolize
- # if defined? Rails::Railtie
- # require 'rails'
- # class Railtie < Rails::Railtie
- # initializer 'symbolize.insert_into_active_record' do
- # ActiveSupport.on_load :active_record do
- # ActiveRecord::Base.extend(Symbolize::ClassMethods)
- # end
- # end
- # end
- # end
+ require 'rails'
+ class Railtie < Rails::Railtie
+ initializer 'symbolize.insert_into_active_record' do
+ ActiveSupport.on_load :active_record do
+ ::ActiveRecord::Base.send :include, Symbolize::ActiveRecord
+ end
+ end
+ end
end
-
-
View
3  spec/spec_helper_ar.rb
@@ -2,8 +2,11 @@
require 'active_record'
require 'symbolize/active_record'
+ActiveRecord::Base.send :include, Symbolize::ActiveRecord # this is normally done by the railtie
+
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:") #'postgresql', :database => 'symbolize_test', :username => 'postgres')
+
if ActiveRecord::VERSION::STRING >= "3.1"
ActiveRecord::Migrator.migrate("spec/db")
else
View
151 spec/symbolize/active_record_spec.rb
@@ -55,67 +55,66 @@ class << ActiveRecord::Base
it "should have a valid blueprint" do
# Test records
u = User.create(:name => 'Bob' , :other => :bar,:status => :inactive, :so => :mac, :gui => :gtk, :language => :en, :sex => false, :cool => false)
- u.errors.messages.should eql({})
+ u.errors.messages.should be_blank
end
it "should work nice with default values from active model" do
u = User.create(:name => 'Niu' , :other => :bar, :so => :mac, :gui => :gtk, :language => :en, :sex => false, :cool => false)
- u.errors.messages.should eql({})
+ u.errors.messages.should be_blank
u.status.should eql(:active)
u.should be_active
end
describe "User Instantiated" do
- before(:each) do
- @user = User.create(:name => 'Anna', :other => :fo, :status => :active , :so => :linux, :gui => :qt, :language => :pt, :sex => true, :cool => true)
- end
-
- it "test_symbolize_string" do
- @user.status = 'inactive'
- @user.status.should eql(:inactive)
+ subject {
+ User.create(:name => 'Anna', :other => :fo, :status => status, :so => so, :gui => :qt, :language => :pt, :sex => true, :cool => true)
+ }
+ let(:status) { :active }
+ let(:so) { :linux }
+
+ describe "test_symbolize_string" do
+ let(:status) { 'inactive' }
+ its(:status) { should == :inactive }
# @user.status_before_type_cast.should eql(:inactive)
# @user.read_attribute(:status).should eql('inactive')
end
- it "test_symbolize_symbol" do
- @user.status = :active
- @user.status.should eql(:active)
- @user.status_before_type_cast.should eql(:active)
+ describe "test_symbolize_symbol" do
+ its(:status) { should == :active }
+ its(:status_before_type_cast) { should == :active }
# @user.read_attribute(:status).should eql('active')
end
- it "should work nice with numbers" do
- @user.status = 43
- @user.status.should_not be_nil
+ describe "should work nice with numbers" do
+ let(:status) { 43 }
+ its(:status) { should be_present }
# @user.status_before_type_cast.should be_nil
# @user.read_attribute(:status).should be_nil
end
- it "should acts nice with nil" do
- @user.status = nil
- @user.status.should be_nil
- @user.status_before_type_cast.should be_nil
- @user.read_attribute(:status).should be_nil
+ describe "should acts nice with nil" do
+ let(:status) { nil }
+ its(:status) { should be_nil }
+ its(:status_before_type_cast) { should be_nil }
+ it { subject.read_attribute(:status).should be_nil }
end
- it "should acts nice with blank" do
- @user.status = ""
- @user.status.should be_nil
- @user.status_before_type_cast.should be_nil
- @user.read_attribute(:status).should be_nil
+ describe "should acts nice with blank" do
+ let(:status) { "" }
+ its(:status) { should be_nil }
+ its(:status_before_type_cast) { should be_nil }
+ it { subject.read_attribute(:status).should be_nil }
end
- it "test_symbols_quoted_id" do
- pending
- @user.status = :active
- @user.status.quoted_id.should eql("'active'")
+ describe "test_symbols_quoted_id" do
+ pending { subject.status.quoted_id.should eql("'active'") }
end
it "should not validates other" do
- @user.other = nil
- @user.should be_valid
- @user.other = ""
- @user.should be_valid
+ subject.other = nil
+ subject.should be_valid
+ subject.other = ""
+ subject.should be_valid
end
it "should get the correct values" do
@@ -123,8 +122,8 @@ class << ActiveRecord::Base
User::STATUS_VALUES.should eql({:inactive=>"Inactive", :active=>"Active"})
end
- it "test_symbolize_humanize" do
- @user.status_text.should eql("Active")
+ describe "test_symbolize_humanize" do
+ its(:status_text) { should eql("Active") }
end
it "should get the correct values" do
@@ -132,8 +131,8 @@ class << ActiveRecord::Base
User::GUI_VALUES.should eql({:cocoa=>"cocoa", :qt=>"qt", :gtk=>"gtk"})
end
- it "test_symbolize_humanize" do
- @user.gui_text.should eql("qt")
+ describe "test_symbolize_humanize" do
+ its(:gui_text) { should eql("qt") }
end
it "should get the correct values" do
@@ -141,58 +140,58 @@ class << ActiveRecord::Base
User::SO_VALUES.should eql({:linux => "Linux", :mac => "Mac OS X", :win => "Videogame"})
end
- it "test_symbolize_humanize" do
- @user.so_text.should eql("Linux")
+ describe "test_symbolize_humanize" do
+ its(:so_text) { should eql("Linux") }
end
- it "test_symbolize_humanize" do
- @user.so = :mac
- @user.so_text.should eql("Mac OS X")
+ describe "test_symbolize_humanize" do
+ let(:so) { :mac }
+ its(:so_text) { should eql("Mac OS X") }
end
it "should stringify" do
- @user.other_text.should eql("fo")
- @user.other = :foo
- @user.other_text.should eql("foo")
+ subject.other_text.should eql("fo")
+ subject.other = :foo
+ subject.other_text.should eql("foo")
end
- it "should validate status" do
- @user.status = nil
- @user.should_not be_valid
- @user.should have(1).errors
+ describe "should validate status" do
+ let(:status) { nil }
+ it { should_not be_valid }
+ it { should have(1).errors }
end
it "should not validate so" do
- @user.so = nil
- @user.should be_valid
+ subject.so = nil
+ subject.should be_valid
end
it "test_symbols_with_weird_chars_quoted_id" do
- @user.status = :"weird'; chars"
- @user.status_before_type_cast.should eql(:"weird'; chars")
+ subject.status = :"weird'; chars"
+ subject.status_before_type_cast.should eql(:"weird'; chars")
# assert_equal "weird'; chars", @user.read_attribute(:status)
# assert_equal "'weird''; chars'", @user.status.quoted_id
end
it "should work fine through relations" do
- @user.extras.create(:key => :one)
+ subject.extras.create(:key => :one)
UserExtra.first.key.should eql(:one)
end
it "should play fine with null db columns" do
- new_extra = @user.extras.build
+ new_extra = subject.extras.build
new_extra.should_not be_valid
end
it "should play fine with null db columns" do
- new_extra = @user.extras.build
+ new_extra = subject.extras.build
new_extra.should_not be_valid
end
describe "i18n" do
it "should test i18n ones" do
- @user.language_text.should eql("Português")
+ subject.language_text.should eql("Português")
end
it "should get the correct values" do
@@ -204,9 +203,9 @@ class << ActiveRecord::Base
end
it "should test boolean" do
- @user.sex_text.should eql("Feminino")
- @user.sex = false
- @user.sex_text.should eql('Masculino')
+ subject.sex_text.should eql("Feminino")
+ subject.sex = false
+ subject.sex_text.should eql('Masculino')
end
it "should get the correct values" do
@@ -232,20 +231,20 @@ class << ActiveRecord::Base
describe "Methods" do
it "should play nice with other stuff" do
- @user.karma.should be_nil
+ subject.karma.should be_nil
User::KARMA_VALUES.should eql({:bad => "bad", :ugly => "ugly", :good => "good"})
end
it "should provide a boolean method" do
- @user.should_not be_good
- @user.karma = :ugly
- @user.should be_ugly
+ subject.should_not be_good
+ subject.karma = :ugly
+ subject.should be_ugly
end
it "should work" do
- @user.karma = "good"
- @user.should be_good
- @user.should_not be_bad
+ subject.karma = "good"
+ subject.should be_good
+ subject.should_not be_bad
end
end
@@ -253,21 +252,21 @@ class << ActiveRecord::Base
describe "Methods" do
it "is dirty if you change the attribute value" do
- @user.language.should == :pt
- @user.language_changed?.should be_false
+ subject.language.should == :pt
+ subject.language_changed?.should be_false
- return_value = @user.language = :en
+ return_value = subject.language = :en
return_value.should == :en
- @user.language_changed?.should be_true
+ subject.language_changed?.should be_true
end
it "is not dirty if you set the attribute value to the same value" do
- @user.language.should == :pt
- @user.language_changed?.should be_false
+ subject.language.should == :pt
+ subject.language_changed?.should be_false
- return_value = @user.language = :pt
+ return_value = subject.language = :pt
return_value.should == :pt
- @user.language_changed?.should be_false
+ subject.language_changed?.should be_false
end
end
View
2  symbolize.gemspec
@@ -26,5 +26,5 @@ Gem::Specification.new do |s|
s.add_development_dependency("bson_ext", ["~> 1.5.0"])
s.add_development_dependency("sqlite3", ["~> 1.3.0"])
s.add_development_dependency("pg", ["~> 0.12.2"])
- s.add_development_dependency("activerecord", ["~> 3.1.0"])
+ s.add_development_dependency("activerecord", [">= 3.1.0", "< 5"])
end
Please sign in to comment.
Something went wrong with that request. Please try again.