Module: Adva::BelongsToAuthor::ClassMethods

Defined in:
lib/adva/belongs_to_author.rb

Instance Method Summary collapse

Instance Method Details

#belongs_to_author(*args) ⇒ Object



41
42
43
44
45
# File 'lib/adva/belongs_to_author.rb', line 41

def belongs_to_author(*args)
  options = args.extract_options!
  args = (args.empty? ? [:author] : args) << options
  belongs_to_user *args
end

#belongs_to_user(*args) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/adva/belongs_to_author.rb', line 11

def belongs_to_user(*args)
  options = args.extract_options!
  args = (args.empty? ? [:user] : args)
  belongs_to_cacheable *args.dup << options # FIXME should not be polymorphic!
  
  args.each do |name|
    class_eval <<-code, __FILE__, __LINE__
      def #{name}_ip
        #{name}.ip if #{name} && #{name}.respond_to?(:ip)
      end

      def #{name}_agent
        #{name}.agent if #{name} && #{name}.respond_to?(:agent)
      end

      def #{name}_referer
        #{name}.referer if #{name} && #{name}.respond_to?(:referer)
      end

      def #{name}_link include_email: true
        if include_email
          %(<a href="mailto:\#{#{name}_email}">\#{#{name}_name}</a>).html_safe
        else
          #{name}_name
        end
      end
    code
  end
end