Module: OneTouch::ActiveRecordExtension::ClassMethods

Defined in:
lib/one_touch/models/active_record_extension.rb

Instance Method Summary collapse

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
[View source]

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

#as_favor_host(opt = {}) ⇒ Object

Used in host class Ex:

as_favor_host :bridge_klass => "AClass"
[View source]

79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/one_touch/models/active_record_extension.rb', line 79

def as_favor_host(opt={})
  include ::OneTouch::PortBuild
  opt.reverse_merge!(bridge_klass: "Favor", no_default_relations: false, portname: :host)
  append_relation(opt)
  # assign_cattr_in_favor_class(:host_klasses,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]
    has_many :favors, :class_name => opt[:bridge_klass], :as => :host
  end
  include AsFavorHost
end

#as_favorable(opt = {}) ⇒ Object

Used in favorable class, so self is a class

[View source]

94
95
96
97
98
99
100
101
102
103
104
# File 'lib/one_touch/models/active_record_extension.rb', line 94

def as_favorable(opt={})
  include ::OneTouch::PortBuild
  opt.reverse_merge!(bridge_klass: "Favor", no_default_relations: false, portname: :favorable)
  append_relation(opt)
  # assign_cattr_in_favor_class(:favorable_klasses,opt)
  unless opt[:no_default_relations]
    has_many :be_favors, :class_name => opt[:bridge_klass], :as => :favorable
  end
  include AsFavorable
  include HostRelation if favor_class.respond_to? :include_host_relation_module and favor_class.include_host_relation_module = true
end