Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add resource_class to Her::Model::ORM #279

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 16 additions & 2 deletions lib/her/model/orm.rb
Expand Up @@ -85,6 +85,20 @@ def destroy
end

module ClassMethods
@@relation_class = Relation
# get the default Relation class
# @ note if unspecified, will return {Her::Model::Relation}
# @private
def relation_class
@@relation_class
end
# set the default Relation class
# @note a custom relation class should inherit or behave like
# {Her::Model::Relation}
def use_relation(klass)
@@relation_class = klass
end

# Create a new chainable scope
#
# @example
Expand All @@ -104,7 +118,7 @@ def scope(name, code)
end

# Add the scope method to the Relation class
Relation.instance_eval do
relation_class.instance_eval do
define_method(name) { |*args| instance_exec(*args, &code) }
end
end
Expand Down Expand Up @@ -197,7 +211,7 @@ def build(attributes = {})
private
# @private
def blank_relation
@blank_relation ||= Relation.new(self)
@blank_relation ||= relation_class.new(self)
end
end
end
Expand Down
45 changes: 44 additions & 1 deletion spec/model/orm_spec.rb
Expand Up @@ -443,7 +443,7 @@ def friends
context 'for children class' do
before do
class User < Foo::User; end
@spawned_models << :User
@spawned_classes << :User
end

it 'uses the custom method (PUT) instead of default method (POST)' do
Expand Down Expand Up @@ -476,4 +476,47 @@ class User < Foo::User; end
end
end
end

context 'without custom relation' do
before do
spawn_model 'User'
#Her::Model::Relation.any_instance.stub(:fetch).and_return(:correct_relation_called)
end

describe '.relation_class' do
it 'should default to Her::Model::Relation' do
User.relation_class.should == Her::Model::Relation
end
end

describe 'scope and relation calls' do
it 'should use the default relation' do
User.scoped.class.should == Her::Model::Relation
end
end

end

context 'with custom relation' do
before do
spawn_relation 'CustomRelation'
spawn_model 'User' do
use_relation CustomRelation
end
end

describe '.relation_class' do
it 'should be the custom relation' do
User.relation_class.should == CustomRelation
end
end

describe 'scope and relation calls' do
it 'should use the custom relation' do
User.scoped.class.should == CustomRelation
end
end

end

end
4 changes: 2 additions & 2 deletions spec/model/parse_spec.rb
Expand Up @@ -54,7 +54,7 @@
spawn_model("Foo::Model") { include_root_in_json true }

class User < Foo::Model; end
@spawned_models << :User
@spawned_classes << :User
end

it "wraps params with the class name" do
Expand Down Expand Up @@ -143,7 +143,7 @@ class User < Foo::Model
collection_path "/users"
end

@spawned_models << :User
@spawned_classes << :User
end

it "parse the data with the symbol" do
Expand Down
2 changes: 1 addition & 1 deletion spec/model/paths_spec.rb
Expand Up @@ -138,7 +138,7 @@
spawn_model("Foo::Model") { include_root_in_json true }

class User < Foo::Model; end
@spawned_models << :User
@spawned_classes << :User
end

it "builds path using the children model name" do
Expand Down
2 changes: 1 addition & 1 deletion spec/model/relation_spec.rb
Expand Up @@ -67,7 +67,7 @@
end

class User < Foo::Model; end
@spawned_models << :User
@spawned_classes << :User
end

it "propagates the scopes through its children" do
Expand Down
2 changes: 1 addition & 1 deletion spec/model/validations_spec.rb
Expand Up @@ -31,7 +31,7 @@ def errors
end

class User < Foo::Model; end
@spawned_models << :User
@spawned_classes << :User
end

it "validates attributes when calling #valid?" do
Expand Down
7 changes: 4 additions & 3 deletions spec/spec_helper.rb
Expand Up @@ -12,15 +12,16 @@

RSpec.configure do |config|
config.include Her::Testing::Macros::ModelMacros
config.include Her::Testing::Macros::RelationMacros
config.include Her::Testing::Macros::RequestMacros

config.before :each do
@spawned_models = []
@spawned_classes = []
end

config.after :each do
@spawned_models.each do |model|
Object.instance_eval { remove_const model } if Object.const_defined?(model)
@spawned_classes.each do |klass|
Object.instance_eval { remove_const klass } if Object.const_defined?(klass)
end
end
end
4 changes: 2 additions & 2 deletions spec/support/macros/model_macros.rb
Expand Up @@ -14,13 +14,13 @@ def spawn_model(klass, &block)
submodel.class_eval(&block) if block_given?
end

@spawned_models << base
@spawned_classes << base
else
Object.instance_eval { remove_const klass } if Object.const_defined?(klass)
Object.const_set(klass, Class.new).send(:include, Her::Model)
Object.const_get(klass).class_eval(&block) if block_given?

@spawned_models << klass.to_sym
@spawned_classes << klass.to_sym
end
end
end
Expand Down
28 changes: 28 additions & 0 deletions spec/support/macros/relation_macros.rb
@@ -0,0 +1,28 @@
module Her
module Testing
module Macros
module RelationMacros
# Create a class and automatically inherit Her::Model::Relation
def spawn_relation(klass, &block)
if klass =~ /::/
base, submodel = klass.split(/::/).map{ |s| s.to_sym }
Object.const_set(base, Module.new) unless Object.const_defined?(base)
Object.const_get(base).module_eval do
remove_const submodel if constants.map(&:to_sym).include?(submodel)
submodel = const_set(submodel, Class.new(Her::Model::Relation))
submodel.class_eval(&block) if block_given?
end

@spawned_classes << base
else
Object.instance_eval { remove_const klass } if Object.const_defined?(klass)
Object.const_set(klass, Class.new(Her::Model::Relation))
Object.const_get(klass).class_eval(&block) if block_given?

@spawned_classes << klass.to_sym
end
end
end
end
end
end