Method: OneTouch::ActiveRecordExtension::ClassMethods#acts_as_favor
- Defined in:
- lib/one_touch/models/active_record_extension.rb
#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
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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/one_touch/models/active_record_extension.rb', line 28 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, :instance_writer => false # method_name_of_host_klasses = "host_klasses_for_#{name.underscore}" 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 AfterAsFavor # TODO: # I should add more files to make here more clear # Some methods should be include first # And others should be include later # The following code should be in another file if opt[:include_host_relation_module] class_attribute :include_host_relation_module, :instance_writer => false self.include_host_relation_module = true add_host_relation_map(relation_recorder.port_group[:favorable], :be_favors, :host) end relation_recorder.port_group[:host].each do |klassname| klass = klassname.to_s.constantize klass.after_load_acts_as_favor if klass.respond_to? :after_load_acts_as_favor end end |