Skip to content
This repository has been archived by the owner on Jul 27, 2021. It is now read-only.

Commit

Permalink
関連モデル呼び出し時にエイリアスモデルが参照されてしまう不具合を修正
Browse files Browse the repository at this point in the history
  • Loading branch information
YOSHIDA Hiroki committed Jul 2, 2014
1 parent b106424 commit 32b5558
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 9 deletions.
4 changes: 2 additions & 2 deletions app/models/comable/cart_item.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Comable
class CartItem < ActiveRecord::Base
belongs_to :customer, class_name: Comable::Customer.model.name, foreign_key: Comable::Customer.foreign_key
belongs_to :comable_stock, class_name: Comable::Stock.model.name, foreign_key: Comable::Stock.foreign_key
belongs_to :customer, class_name: Comable::Customer.model_name, foreign_key: Comable::Customer.foreign_key
belongs_to :comable_stock, class_name: Comable::Stock.model_name, foreign_key: Comable::Stock.foreign_key

validates Comable::Customer.foreign_key, uniqueness: { scope: [Comable::Customer.foreign_key, Comable::Stock.foreign_key] }

Expand Down
2 changes: 1 addition & 1 deletion app/models/comable/order.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Comable
class Order < ActiveRecord::Base
belongs_to :customer, class_name: Comable::Customer.model.name, foreign_key: Comable::Customer.foreign_key, autosave: false
belongs_to :customer, class_name: Comable::Customer.model_name, foreign_key: Comable::Customer.foreign_key, autosave: false
has_many :comable_order_deliveries, dependent: :destroy, class_name: 'Comable::OrderDelivery', foreign_key: 'comable_order_id'

accepts_nested_attributes_for :comable_order_deliveries
Expand Down
2 changes: 1 addition & 1 deletion app/models/comable/order_detail.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Comable
class OrderDetail < ActiveRecord::Base
belongs_to :comable_stock, class_name: Comable::Stock.model.name, foreign_key: Comable::Stock.foreign_key
belongs_to :comable_stock, class_name: Comable::Stock.model_name, foreign_key: Comable::Stock.foreign_key
belongs_to :comable_order_deliveries, class_name: 'Comable::OrderDelivery'

after_create :decrement_stock
Expand Down
2 changes: 1 addition & 1 deletion lib/acts_as_comable_product/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def self.included(base)

module ClassMethods
def acts_as_comable_product
has_many :comable_stocks, class_name: Comable::Stock.model.name
has_many :comable_stocks, class_name: Comable::Stock.model_name

after_create :create_stock

Expand Down
2 changes: 1 addition & 1 deletion lib/acts_as_comable_stock/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def self.included(base)

module ClassMethods
def acts_as_comable_stock
belongs_to :comable_product, class_name: Comable::Product.model.name, foreign_key: Comable::Product.foreign_key
belongs_to :comable_product, class_name: Comable::Product.model_name, foreign_key: Comable::Product.foreign_key

scope :activated, -> { where.not(product_id_num: nil) }
scope :unsold, -> { where('quantity > ?', 0) }
Expand Down
10 changes: 7 additions & 3 deletions lib/comable/mapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@ def new(*args, &block)
model.comable(comable_type).new(*args, &block)
end

def model
def model_name
# TODO: テーブル名とモデル名が異なるケースに対応できるよう検討
"::#{Comable::Engine.config.send("#{comable_type}_table").to_s.classify}".constantize
"::#{Comable::Engine.config.send("#{comable_type}_table").to_s.classify}"
end

def model
model_name.constantize
end

def foreign_key
"#{model_name.singular}_id"
"#{model.model_name.singular}_id"
end

private
Expand Down

0 comments on commit 32b5558

Please sign in to comment.