Module: OneTouch::AsFavorHost::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

#after_load_acts_as_favorObject



40
41
42
# File 'lib/one_touch/models/as_favor_host.rb', line 40

def after_load_acts_as_favor
  build_context_klass_methods
end

#build_context_klass_methodsObject

Generate instance methods like

@user.focus_tags
=> return a list of tags (ActiveRelation object) that focus by @user


47
48
49
50
51
52
53
54
55
56
# File 'lib/one_touch/models/as_favor_host.rb', line 47

def build_context_klass_methods
  favor_class.cons_context_match.each do |context, klasses|
    klasses.each do |klass|
      method_name = "#{context}_#{klass.to_s.underscore.pluralize}".to_sym
      define_method method_name do
        klass.to_s.constantize.favored_by(self,context) # TODO: here would cause problem, If klass did not respond_to favored_by? Need a test to prove it
      end
    end
  end
end

#cached_favor_to(favorable, context = default_context_in_host) ⇒ Object

Programmer Doc

This method is not used in otherwhere


33
34
35
36
37
38
# File 'lib/one_touch/models/as_favor_host.rb', line 33

def cached_favor_to(favorable,context=default_context_in_host)
  key = "#{self.name}_favor_to_#{favorable.class.name}_#{favorable.id}_#{context}"
  Rails.cache.fetch key do
    favor_to(favorable, context)
  end
end

#favor_to(favorable, context = default_context_in_host) ⇒ Object

TODO

favor_to(@favorable, focus) could extend to focus_to(@favorable)
favor_to(@favorable, like) could extend to like_to(@favorable)
means add more dynamic methods relies on context

Ex:

User.favor_to(@user1, :follow)
  => return guys(an ActiveRelation object) who follows @user1, so you can define fans


27
28
29
# File 'lib/one_touch/models/as_favor_host.rb', line 27

def favor_to(favorable,context=default_context_in_host)
  host_context_as(context).merge(Favor.favorable_as favorable)
end