-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from hyoshida/hotfix/association
Fix problems for Active Record Association
- Loading branch information
Showing
11 changed files
with
210 additions
and
53 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
class Product < ActiveRecord::Base | ||
has_many :stocks | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class Stock < ActiveRecord::Base | ||
belongs_to :product | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class CreateStocks < ActiveRecord::Migration | ||
def change | ||
create_table :stocks do |t| | ||
t.references :product | ||
t.integer :units | ||
t.timestamps | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
FactoryGirl.define do | ||
factory :product do | ||
title 'My favorite product' | ||
|
||
trait :with_stock | ||
stocks { FactoryGirl.build_list(:stock, 1) } | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
FactoryGirl.define do | ||
factory :stock do | ||
units 10 | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,2 @@ | ||
describe Utusemi::Configuration do | ||
before do | ||
Utusemi.configure do | ||
map :sample do | ||
name :title | ||
end | ||
end | ||
class Product < ActiveRecord::Base; end | ||
end | ||
|
||
subject { Product } | ||
|
||
it { should respond_to(:utusemi) } | ||
|
||
context 'ActiveRecord::Base#utusemi' do | ||
let(:product) { FactoryGirl.build(:product) } | ||
|
||
subject { product.utusemi(:sample) } | ||
|
||
it { should respond_to(:title) } | ||
it { should respond_to(:name) } | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
describe Utusemi::Core do | ||
# TODO: Implement the new syntax | ||
# | ||
# map(:sample_one) { ... } | ||
# map(:sample_two) { ... } | ||
# class Stock < ActiveRecord::Base | ||
# belongs_to :product, utusemi: :sample_one | ||
# end | ||
# class Product < ActiveRecord::Base | ||
# has_many :stocks, utusemi: :sample_two | ||
# end | ||
# product.utusemi.stocks.first | ||
# | ||
describe ActiveRecord::Associations do | ||
before do | ||
Utusemi.configure do | ||
map(:product) { name :title } | ||
map(:stock) { quantity :units } | ||
end | ||
end | ||
|
||
describe '#scope' do | ||
let(:product) { FactoryGirl.create(:product, :with_stock) } | ||
subject { product.reload.utusemi(:product).stocks.first } | ||
it { should respond_to(:units) } | ||
it { should respond_to(:quantity) } | ||
it { expect(subject.units).to eq(subject.quantity) } | ||
end | ||
|
||
describe '#load_target' do | ||
let(:product) { FactoryGirl.build(:product, :with_stock) } | ||
subject { product.utusemi(:product).stocks.first } | ||
it { should respond_to(:units) } | ||
it { should respond_to(:quantity) } | ||
it { expect(subject.units).to eq(subject.quantity) } | ||
end | ||
end | ||
|
||
describe ActiveRecord::Base::ClassMethods do | ||
describe '::utusemi!' do | ||
before { class TemporaryModel < ActiveRecord::Base; end } | ||
before { subject.utusemi! } | ||
subject { TemporaryModel } | ||
it { expect(subject.utusemi_values).not_to be_empty } | ||
end | ||
|
||
describe '::utusemi' do | ||
before { subject.utusemi } | ||
subject { Product } | ||
it { expect(subject.utusemi_values).to be_empty } | ||
end | ||
end | ||
|
||
describe Utusemi::Core::InstanceMethods do | ||
let(:product_first) { FactoryGirl.build(:product) } | ||
let(:product_second) { FactoryGirl.build(:product) } | ||
|
||
describe '#utusemi!' do | ||
before { subject.utusemi! } | ||
subject { product_first } | ||
it { expect(subject.utusemi_values).not_to be_empty } | ||
end | ||
|
||
describe '#utusemi' do | ||
before { subject.utusemi } | ||
subject { product_second } | ||
it { expect(subject.utusemi_values).to be_empty } | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,56 @@ | ||
describe Product, type: :model do | ||
pending "add some examples to (or delete) #{__FILE__}" | ||
describe Product do | ||
let(:product) { FactoryGirl.build(:product) } | ||
|
||
before do | ||
Utusemi.configure do | ||
map :sample do |options| | ||
name :title | ||
caption options[:caption] || :none | ||
end | ||
end | ||
end | ||
|
||
it { should respond_to(:utusemi) } | ||
|
||
# TODO: Implement the new syntax | ||
# | ||
# map(:product) { ... } | ||
# Product.utusemi.first | ||
# | ||
describe '#utusemi(type)' do | ||
subject { product.utusemi(:sample) } | ||
it { should respond_to(:title) } | ||
it { should respond_to(:name) } | ||
end | ||
|
||
describe '#utusemi(type, options)' do | ||
subject { product.utusemi(:sample, caption: :title) } | ||
it { expect(subject.caption).to eq(subject.title) } | ||
end | ||
|
||
describe '::utusemi(type)' do | ||
before { FactoryGirl.create(:product, title: 'foobar') } | ||
subject { described_class.utusemi(:sample) } | ||
|
||
it '::where by alias column' do | ||
expect(subject.where(name: 'foobar').count).to eq(1) | ||
end | ||
|
||
it '::order by alias column' do | ||
expect { subject.order(:name) }.not_to raise_error | ||
end | ||
|
||
it 'call alias column from instance' do | ||
expect(subject.first.name).to eq(subject.first.title) | ||
end | ||
end | ||
|
||
describe '::utusemi(type, options)' do | ||
before { FactoryGirl.create(:product, title: 'foobar') } | ||
subject { described_class.utusemi(:sample, caption: :title) } | ||
|
||
it 'call alias column from instance' do | ||
expect(subject.first.caption).to eq(subject.first.title) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe Stock, type: :model do | ||
pending "add some examples to (or delete) #{__FILE__}" | ||
end |