-
-
Save mbj/4135699 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 User, '.with_first_name' do | |
let(:object) { described_class } | |
subject { object.with_first_name(name) } | |
let(:result) { mock('Result') } | |
let(:name) { mock('Name') } | |
before do | |
User.stub(:all => result) | |
end | |
it 'should return result' do | |
should be(result) | |
end | |
it 'should generate dm-1 query correctly' do | |
User.should_receive(:all).with(:firstname => name).and_return(result) | |
should be(result) | |
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
## Model | |
class User | |
include DataMapper::Resource | |
property :id, Serial | |
property :first_name, String | |
property :last_name, String | |
end | |
## How manage his query interface. Doing directly in User class and combine them ex : | |
# | |
class User | |
include DataMapper::Resource | |
property :id, Serial | |
property :first_name, String | |
property :last_name, String | |
def self.with_first_name(name) | |
all({:first_name => name}) | |
end | |
def self.with_last_name(name) | |
all({:last_name => name}) | |
end | |
end | |
User.with_first_name(name).with_last_name(name) | |
## Or create a new class to manage all Query stuff like : | |
class UserRequest | |
def self.with_first_name(name) | |
User.all({:first_name => name}) | |
end | |
def self.with_last_name(name) | |
User.all({:last_name => name}) | |
end | |
end | |
UserRequest.with_first_name(name).with_last_name(name) | |
## The second solution avoid put method in your model and separate your Query to your model |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment