Module: OneTouch::ActiveRecordExtension::ClassMethods
- Defined in:
- lib/one_touch/models/active_record_extension.rb
Overview
methods added here are added to ActiveRecord::Base, so make it as thin as possible
Instance Method Summary collapse
-
#acts_as_favor(opt = {}) ⇒ Object
Default is define two relations: belongs_to :host, :class_name => “User” belongs_to :favorable, :polymorphic => true Set acts_as_favor :host_class_name => nil to override default host relation by belongs_to :host, :polymorphic => true User can define his own specific relations, but make sure two relations name must be host and favorable and use acts_as_favor :no_default_relations => false after user defined relations Ex: belongs_to :host, :class_name => “AnyOne”, :primary_key => :any_key belongs_to :tag acts_as_favor :no_default_relations => true.
-
#as_favor_host(opt = {}) ⇒ Object
Used in host class Ex: as_favor_host :bridge_klass => “AClass”.
-
#as_favorable(opt = {}) ⇒ Object
Used in favorable class, so self is a class.
Instance Method Details
#acts_as_favor(opt = {}) ⇒ Object
Default is define two relations:
belongs_to :host, :class_name => "User"
belongs_to :favorable, :polymorphic => true
Set acts_as_favor :host_class_name => nil to override default host relation by
belongs_to :host, :polymorphic => true
User can define his own specific relations, but make sure two relations name must be host and favorable
and use acts_as_favor :no_default_relations => false after user defined relations
Ex: belongs_to :host, :class_name => "AnyOne", :primary_key => :any_key
belongs_to :tag
acts_as_favor :no_default_relations => true
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/one_touch/models/active_record_extension.rb', line 25 def acts_as_favor(opt={}) include ::OneTouch::Bridge opt.reverse_merge!({:include_host_relation_module => true, :host_class_name => "User", :no_default_relations => false}) class_attribute :host_relation_type, :favorable_relation_type, :include_host_relation_module, :instance_writer => false self.include_host_relation_module = opt[:include_host_relation_module] include ActsAsFavor unless opt[:no_default_relations] if opt[:host_class_name].nil? belongs_to :host, :polymorphic => true self.host_relation_type = :polymorphic else belongs_to :host, :class_name => opt[:host_class_name] self.host_relation_type = :single end if opt[:favorable_class_name].nil? belongs_to :favorable, :polymorphic => true self.favorable_relation_type = :polymorphic else belongs_to :favorable, :class_name => opt[:favorable_class_name] self.favorable_relation_type = :single end end # include Relation model and make favorable classes also include HostRelation if needed # make sure the former loaded host class run method after_load_acts_as_favor include AfterAsFavor end |
#as_favor_host(opt = {}) ⇒ Object
Used in host class Ex:
as_favor_host :bridge_klass => "AClass"
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/one_touch/models/active_record_extension.rb', line 60 def as_favor_host(opt={}) include ::OneTouch::PortBuild opt.reverse_merge!(bridge_klass: "Favor", no_default_relations: false, portname: :host, polymorphic: false) append_relation(opt) # Programmer Doc # The Name of has_many is stick to favors, to make it changable would cause some query problem # :as => :host can not be configured too, cause in act_as_favor the relation name was sticked unless opt[:no_default_relations] if opt[:polymorphic] == true has_many :favors, :class_name => opt[:bridge_klass], :as => :host else has_many :favors, :class_name => opt[:bridge_klass], :foreign_key => :host_id end end include AsFavorHost end |
#as_favorable(opt = {}) ⇒ Object
Used in favorable class, so self is a class
78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/one_touch/models/active_record_extension.rb', line 78 def as_favorable(opt={}) include ::OneTouch::PortBuild opt.reverse_merge!(bridge_klass: "Favor", no_default_relations: false, portname: :favorable, polymorphic: true) append_relation(opt) unless opt[:no_default_relations] if opt[:polymorphic] == true has_many :be_favors, :class_name => opt[:bridge_klass], :as => :favorable else has_many :be_favors, :class_name => opt[:bridge_klass] # we need add option of foreign key end end include AsFavorable include HostRelation if favor_class.respond_to? :include_host_relation_module and favor_class.include_host_relation_module end |