Permalink
Browse files

Changed enumify tests to use activerecord with in-memory sqlite datab…

…ase.

This enables it to test scopes and other activerecord features that are not present in supermodel.
Added not_scopes.
  • Loading branch information...
1 parent b1650ac commit c419e65ca6af57c9da7e63a91d262b8b076c26e6 @ifeins committed Apr 10, 2012
Showing with 68 additions and 8 deletions.
  1. +2 −1 enumify.gemspec
  2. +5 −0 lib/enumify/model.rb
  3. +42 −6 spec/enumify/enum_spec.rb
  4. +19 −1 spec/spec_helper.rb
View
3 enumify.gemspec
@@ -21,5 +21,6 @@ Gem::Specification.new do |s|
# specify any dependencies here; for example:
s.add_development_dependency "rake"
s.add_development_dependency "rspec"
- s.add_development_dependency "supermodel"
+ s.add_development_dependency "activerecord"
+ s.add_development_dependency "sqlite3"
end
View
5 lib/enumify/model.rb
@@ -40,7 +40,12 @@ def enum(parameter, opts=[])
send("_set_#{parameter.to_s}", opt, true)
end
+
scope opt.to_sym, where(parameter.to_sym => opt.to_s)
+ # We need to prefix the field with the table name since if this scope will
+ # be used in a joined query with other models that have the same field then
+ # it will fail on ambiguous column name.
+ scope "not_#{opt}".to_sym, where("#{self.table_name}.#{parameter} != ?", opt.to_s)
end
end
View
48 spec/enumify/enum_spec.rb
@@ -1,19 +1,35 @@
require 'spec_helper'
-class Model < SuperModel::Base
- include ActiveModel::Validations
+class Model < ActiveRecord::Base
extend Enumify::Model
- def self.scope(name,hash={}) self end
- def self.where(hash={}) self end
enum :status, [:available, :canceled, :completed]
-
+end
+
+class OtherModel < ActiveRecord::Base
+ extend Enumify::Model
+
+ belongs_to :model
+
+ enum :status, [:active, :expired]
end
describe :Enumify do
before(:each) do
- @obj = Model.new(:status => :available)
+ Model.delete_all
+
+ @obj = Model.create!(:status => :available)
+ @canceled_obj = Model.create!(:status => :canceled)
+ @completed_obj = Model.create!(:status => :completed)
+
+ @active_obj = OtherModel.create!(:status => :active)
+ @active_obj.model = @obj
+ @active_obj.save!
+
+ @expired_obj = OtherModel.create!(:status => :expired)
+ @expired_obj.model = @canceled_obj
+ @expired_obj.save!
end
describe "short hand methods" do
@@ -97,6 +113,26 @@ def self.where(hash={}) self end
end
+ describe "scopes" do
+ it "should return objects with given status" do
+ Model.available.should == [@obj]
+ Model.canceled.should == [@canceled_obj]
+ end
+
+ it "should return objects with given status when joined with models who have the same enum field" do
+ OtherModel.joins(:model).active.should == [@active_obj]
+ end
+
+ it "should return objects that do not have given status" do
+ Model.not_available.should include(@canceled_obj, @completed_obj)
+ end
+
+ it "should return objects that do not have given status when joined with models who have the same enum field" do
+ OtherModel.joins(:model).not_active.should == [@expired_obj]
+ end
+
+ end
+
it "class should have a CONST that holds all the available options of the enum" do
Model::STATUSES.should == [:available, :canceled, :completed]
end
View
20 spec/spec_helper.rb
@@ -1,2 +1,20 @@
require 'enumify'
-require 'supermodel'
+require 'active_record'
+
+def set_database
+ db_config = {:adapter => "sqlite3", :database => ":memory:"}
+ ActiveRecord::Base.establish_connection(db_config)
+ connection = ActiveRecord::Base.connection
+
+ connection.create_table :models do |t|
+ t.string :status
+ end
+
+ connection.create_table :other_models do |t|
+ t.string :status
+ t.references :model
+ end
+end
+
+set_database
+

0 comments on commit c419e65

Please sign in to comment.