Module: OneTouch::ActsAsFavor::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

#build_by_three_elements(h, f, context) ⇒ Object



134
135
136
# File 'lib/one_touch/models/acts_as_favor.rb', line 134

def build_by_three_elements(h, f, context)
  new(:host => h, :favorable => f, :context => context)
end

#favorable_klassesObject



130
131
132
# File 'lib/one_touch/models/acts_as_favor.rb', line 130

def favorable_klasses
  relation_recorder.port_group[:favorable]
end

#generate_add_methods(name) ⇒ Object

Generate dynamic class methods like add_focus, add_follow Favor.add_focus has at least two parameters as add_focus(operator,resource)

the operator is someone(maybe something) who has taken an action(context) on a resource
the resource is target
return an object with error when create favor failed

Even method could receive a block to assign more attributes, but it is not recommended Favor is designed to handle simple data structure, mostly for simple relation data

just the user click one button or touch one link, then a favor was created


74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/one_touch/models/acts_as_favor.rb', line 74

def generate_add_methods(name)
  method_name = "add_#{name}".to_sym
  define_singleton_method method_name do | operator, resource, other_field_option={} |
    build_hash = other_field_option.merge(context: name)
    favor = operator.favors.build build_hash
    favor.favorable = resource
    yield favor if block_given?
    if favor.save
      favor_to_key = "#{resource.class.name}_favor_to_#{resource.class.name}_#{resource.id}_#{name}"
      ::Rails.cache.delete favor_to_key
    end
    favor # return saved favor, favor.error should not be blank when save is false
  end
  method_name = "add_#{name}!".to_sym
  define_singleton_method method_name do | operator, resource, other_field_option={} |
    if block_given?
      result = send "add_#{name}", operator, resource, other_field_option, &block
    else
      result = send "add_#{name}", operator, resource, other_field_option
    end
    if result.errors.present?
      raise(ActiveRecord::RecordInvalid.new(result))
    else
      result
    end
  end
end

#generate_del_methods(name) ⇒ Object

Generate dynamic class methods like del_focus, del_follow Most like add_focus, but raise errors when delete favor failed

TODO: add exception object when raise errors or use the same raise of destroy


105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/one_touch/models/acts_as_favor.rb', line 105

def generate_del_methods(name)
  method_name = "del_#{name}".to_sym
  define_singleton_method method_name do |ope,res,other_field_option={}|
    query_hash = other_field_option.merge(context: name)
    favor = ope.favors.where(query_hash).favorable_as(res)
    if favor.present?
      favor_to_key = "#{res.class.name}_favor_to_#{res.class.name}_#{res.id}_#{name}"
      ::Rails.cache.delete favor_to_key
      favor.first.destroy
    else
      false
      # raise "delete favor failed,can not find favor object",caller
    end
  end
end

#host_klassesObject

seems just used for OneTouch::Relations module



126
127
128
# File 'lib/one_touch/models/acts_as_favor.rb', line 126

def host_klasses
  relation_recorder.port_group[:host]
end

#relation_recorderObject



121
122
123
# File 'lib/one_touch/models/acts_as_favor.rb', line 121

def relation_recorder
  RelationRecorder.find(self.name)
end