Class: Relation

Inherits:
Object
  • Object
show all
Defined in:
lib/scaffold/lib/model/relations/relation.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Relation

Returns a new instance of Relation.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/scaffold/lib/model/relations/relation.rb', line 11

def initialize(options)
  default = {
    class: options[:table_name].singularize.capitalize.constantize,
    select_line: "*",
    table_name: "",
    where_line: "",
    where_vals: [],
    join_line: ""
  }

  default.merge!(options)
  @table_name = default[:table_name]
  @where_line = default[:where_line]
  @where_vals = default[:where_vals]
  @obj_class = default[:class]
  @join_line = default[:join_line]
  @select_line = default[:select_line]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object



115
116
117
# File 'lib/scaffold/lib/model/relations/relation.rb', line 115

def method_missing(m, *args, &block)
  parsed_query.send(m, *args, &block)
end

Instance Attribute Details

#join_lineObject

Returns the value of attribute join_line.



2
3
4
# File 'lib/scaffold/lib/model/relations/relation.rb', line 2

def join_line
  @join_line
end

#obj_classObject (readonly)

Returns the value of attribute obj_class.



2
3
4
# File 'lib/scaffold/lib/model/relations/relation.rb', line 2

def obj_class
  @obj_class
end

#select_lineObject

Returns the value of attribute select_line.



2
3
4
# File 'lib/scaffold/lib/model/relations/relation.rb', line 2

def select_line
  @select_line
end

#table_nameObject (readonly)

Returns the value of attribute table_name.



2
3
4
# File 'lib/scaffold/lib/model/relations/relation.rb', line 2

def table_name
  @table_name
end

#where_lineObject

Returns the value of attribute where_line.



2
3
4
# File 'lib/scaffold/lib/model/relations/relation.rb', line 2

def where_line
  @where_line
end

#where_valsObject

Returns the value of attribute where_vals.



2
3
4
# File 'lib/scaffold/lib/model/relations/relation.rb', line 2

def where_vals
  @where_vals
end

Instance Method Details

#includes(assoc) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/scaffold/lib/model/relations/relation.rb', line 60

def includes(assoc)
  result_array = []
  result_hash = Hash.new { |h, k| h[k] = [] }
  join_options = obj_class.assoc_options[assoc]
  join_table_name = join_options.table_name
  join_class = join_options.model_class
  
  left_joins(join_table_name)
  data = query
  obj_params_length = obj_class.columns.length

  data.each do |el|
    obj = parse_all([el.take(obj_params_length)]).first
    join_arr = el.drop(obj_params_length)
    join_obj = join_class.parse_all([join_arr]).first

    result_array << obj if result_hash[obj.id].empty?
    result_hash[obj.id] << join_obj unless join_arr.all?(&:nil?)
  end

  [result_array, result_hash]
end

#joins(name) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/scaffold/lib/model/relations/relation.rb', line 83

def joins(name)
  join_options = obj_class.assoc_options.values
    .find { |options| options.table_name == name.to_s }

  if join_options.is_a?(BelongsToOptions)
    table_id = join_options.foreign_key
    join_table_id = join_options.primary_key
  else
    table_id = join_options.primary_key
    join_table_id = join_options.foreign_key
  end

  self.join_line = "JOIN #{join_options.table_name} on #{join_options.table_name}.#{join_table_id} = #{table_name}.#{table_id}"
  self
end

#left_joins(name) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/scaffold/lib/model/relations/relation.rb', line 99

def left_joins(name)
  join_options = obj_class.assoc_options.values
    .find { |options| options.table_name == name.to_s }
    
  if join_options.is_a?(BelongsToOptions)
    table_id = join_options.foreign_key
    join_table_id = join_options.primary_key
  else
    table_id = join_options.primary_key
    join_table_id = join_options.foreign_key
  end

  self.join_line = "LEFT OUTER JOIN #{join_options.table_name} on #{join_options.table_name}.#{join_table_id} = #{table_name}.#{table_id}"
  self
end

#parsed_queryObject



119
120
121
# File 'lib/scaffold/lib/model/relations/relation.rb', line 119

def parsed_query
  parse_all(query)
end

#queryObject



123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/scaffold/lib/model/relations/relation.rb', line 123

def query
  result = DBConnection.execute(<<-SQL, where_vals)
    SELECT
      #{select_line}
    FROM
      #{table_name}
    #{join_line}
    #{where_line.blank? ? "" : "WHERE"}
      #{where_line}
  SQL

  result
end

#select(*params) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/scaffold/lib/model/relations/relation.rb', line 30

def select(*params)
  if params.length == 1 && params.first.is_a?(String)
    new_line = params.first
  else
    new_line = select_string(*params)
  end

  self.select_line = new_line
  self
end

#where(params) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/scaffold/lib/model/relations/relation.rb', line 41

def where(params)
  if params.is_a?(Hash)
    new_line = params_string(params)
    new_vals = params.values
  else
    new_line = params
    new_vals = []
  end
  
  if where_line.blank?
    self.where_line = new_line
  else
    self.where_line = "#{where_line} AND #{new_line}"
  end

  self.where_vals = where_vals + new_vals
  self
end