Created
September 2, 2010 12:12
-
-
Save anonymous/562203 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe HasAndBelongsToManyActiveResource do | |
class Resource | |
end | |
class Record | |
include HasAndBelongsToManyActiveResource | |
end | |
before(:each) do | |
@resource = Resource.new | |
@resource.stub(:id).and_return('1') | |
Resource.should_receive(:find).and_return([@resource]) # fails here | |
Resource.should_receive(:new).and_return(@resource) | |
@record = Record.new | |
@record.stub(:id).and_return('2') | |
@record.stub(:resource_ids).and_return([1]) | |
@record.stub(:current_language).and_return('de') | |
Record.should_receive(:serialize).and_return(true) | |
Record.should_receive(:new).and_return(@record) | |
end | |
if "should ..." | |
@record.class.has_and_belongs_to_many_active_resource :resourcen | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Spec::Mocks::MockExpectationError in 'HasAndBelongsToManyActiveResource should ...' | |
<Resource (class)> expected :find with (any args) once, but received it 0 times | |
./spec/models/module_spec.rb:17: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module HasAndBelongsToManyActiveResource | |
def self.included(base) | |
def base.has_and_belongs_to_many_active_resource resource, options={} | |
resource = resource.to_s.singularize | |
attribute = options[:attribute] || resource.foreign_key.pluralize | |
klass = (options[:class_name] || resource.classify).constantize | |
self.serialize attribute | |
define_method resource.pluralize do |person_id| | |
values = self.send attribute | |
return [] if values.blank? | |
klass.find :all, :params => { :ids => values, :person_id => person_id, :language => current_language } | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe HasAndBelongsToManyActiveResource do | |
class Resource | |
end | |
class Record | |
include HasAndBelongsToManyActiveResource | |
end | |
before(:each) do | |
@resource = Resource.new | |
@resource.stub(:id).and_return('1') | |
Resource.stub(:find).and_return([@resource]) # fails here | |
Resource.stub(:new).and_return(@resource) | |
@record = Record.new | |
@record.stub(:id).and_return('2') | |
@record.stub(:resource_ids).and_return([1]) | |
@record.stub(:current_language).and_return('de') | |
Record.stub(:serialize).and_return(true) | |
Record.stub(:new).and_return(@record) | |
end | |
if "should ..." | |
@record.class.has_and_belongs_to_many_active_resource :resourcen | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment