Module: ActiveTsv::Reflection

Included in:
Base
Defined in:
lib/active_tsv/reflection.rb

Instance Method Summary collapse

Instance Method Details

#belongs_to(name) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/active_tsv/reflection.rb', line 33

def belongs_to(name)
  class_eval <<-CODE, __FILE__, __LINE__ + 1
    def #{name}
      #{name.to_s.classify}.where(self.class.primary_key => self["#{name}_id"]).first
    end
  CODE
end

#has_many(name, through: nil) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/active_tsv/reflection.rb', line 3

def has_many(name, through: nil)
  if through
    class_eval <<-CODE, __FILE__, __LINE__ + 1
      def #{name}
        #{name.to_s.classify}.where(
          #{name.to_s.classify}.primary_key => #{through}.pluck(:#{name.to_s.singularize.underscore}_id)
        )
      end
    CODE
  else
    class_eval <<-CODE, __FILE__, __LINE__ + 1
      def #{name}
        #{name.to_s.singularize.classify}.where(
          #{self.name.underscore}_id: self[self.class.primary_key]
        )
      end
    CODE
  end
end

#has_one(name) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/active_tsv/reflection.rb', line 23

def has_one(name)
  class_eval <<-CODE, __FILE__, __LINE__ + 1
    def #{name}
      #{name.to_s.singularize.classify}.where(
        #{self.name.underscore}_id: self[self.class.primary_key]
      ).first
    end
  CODE
end