Module: ModelMixins::TableBuilderClassMethods

Defined in:
lib/model_mixins/table_builder_class_methods.rb

Instance Method Summary collapse

Instance Method Details

#check_non_existing_colum_order_by(settings, params) ⇒ Object



253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/model_mixins/table_builder_class_methods.rb', line 253

def check_non_existing_colum_order_by(settings, params)
  order_by_arr = params[:order_by].split(",")
  order_by_arr.each_with_index do |one_order_by, index|
    if one_order_by.match(/^.*?non_existing_column___.*$/i)
      identifier_and_direction = one_order_by.split("non_existing_column___").second
      identifier = identifier_and_direction.split(" ").first
      order_by_direction = identifier_and_direction.split(" ").second
      settings[:columns].each do |col|
        if !col[:select_as].blank? && !col[:format_method].blank? && col[:format_method] == identifier
          order_by_arr[index] = col[:order_by].gsub(",", " #{order_by_direction} ,") + " #{order_by_direction}"
        else
          ""
        end
      end
    else
      ""
    end
  end
  params[:real_order_by] = order_by_arr*","
end

#filter(settings, params, per_page = 10) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/model_mixins/table_builder_class_methods.rb', line 161

def filter(settings, params, per_page = 10)
  order_by = params[:real_order_by]

  cond_str = ""
  cond_hash = {}
  if !params.blank? && params['find']
    params['find'].each_pair do |i, v|
      unless v.blank?
        if i.match(/^.*?non_existing_column___.*$/i)
          identifier = i.split("non_existing_column___").second
          settings[:columns].each do |col|
            if !col[:select_as].blank? && !col[:format_method].blank? && col[:format_method] == identifier
              cond_str += " AND " unless cond_str.blank?
              cond_str += "( "
              sub_cond = ""
              col[:select].split(",").each do |sub_cond_col|
                sub_cond += " OR " unless sub_cond.blank?
                non_existing_column_i = col[:select_as] + "." + sub_cond_col.gsub(" ", "")
                cond_id = "find_#{non_existing_column_i.gsub(/\./, '_')}"
                sub_cond += "#{non_existing_column_i} LIKE :#{cond_id}" #OR guest_email LIKE :find"
                cond_hash.merge!({cond_id.to_sym => "%#{v}%"})
              end
              cond_str += sub_cond + " )"
            else
              ""
            end
          end
        else
          cond_str += " AND " unless cond_str.blank?
          cond_id = "find_#{i.gsub(/\./, '_')}"
          cond_str += "#{i} LIKE :#{cond_id}" #OR guest_email LIKE :find"
          cond_hash.merge!({cond_id.to_sym => "%#{v}%"})
        end
      end
    end
  end

  if !params.blank? && params['multichoice']
    params['multichoice'].each_pair do |i, v|
      unless v.blank?
        cond_str += " AND " unless cond_str.blank?
        cond_id = "multichoice_#{i.gsub(/\./, '_')}"

        cond_str += "#{i} IN (:#{cond_id})" #OR guest_email LIKE :find"
        cond_hash.merge!({cond_id.to_sym => v})
      end
    end
  end

  if !params.blank? && params['date_from']
    params['date_from'].each_pair do |i, v|
      unless v.blank?
        cond_str += " AND " unless cond_str.blank?
        cond_id = "date_from_#{i.gsub(/\./, '_')}"
        cond_str += "#{i} >= :#{cond_id}" #OR guest_email LIKE :find"
        cond_hash.merge!({cond_id.to_sym => "#{v}"})
      end
    end
  end

  if !params.blank? && params['date_to']
    params['date_to'].each_pair do |i, v|
      unless v.blank?
        cond_str += " AND " unless cond_str.blank?
        cond_id = "date_to_#{i.gsub(/\./, '_')}"
        cond_str += "#{i} <= :#{cond_id}" #OR guest_email LIKE :find"
        cond_hash.merge!({cond_id.to_sym => "#{v}"})
      end
    end
  end

  ret = where(cond_str, cond_hash).paginate(:page => params[:page], :per_page => per_page).order(order_by)
  #items = self.joins("LEFT OUTER JOIN intranet_text_pages ON resource_id = intranet_text_pages.id").where(cond_str, cond_hash).paginate(:page => params[:page], :per_page => per_page).order(order_by).selection(settings)
  #if params[:page].to_i > items.total_pages && items.total_pages > 0
  #  params[:page] = 1
  #  items = self.where(cond_str, cond_hash).paginate(:page => params[:page], :per_page => per_page).order(order_by).selection(settings)
  #end
  #items

  # if there are additional joins i will add them
  settings[:columns].each do |col|
    col[:table_primary_key] = "id" if col[:table_primary_key].blank?
    if !col[:join_on].blank?
      col[:select] += ", #{col[:table_primary_key]}" # adding primary key so it can be used in on condition
      ret= ret.joins("LEFT OUTER JOIN (SELECT #{col[:select]} FROM #{col[:table]}) #{col[:select_as]} ON #{col[:select_as]}.#{col[:table_primary_key]}=#{col[:join_on]}")
    end
  end

  ret
end

#prepare_settings(logged_user, object, settings, params, per_page = 10) ⇒ Object



3
4
5
6
7
8
9
10
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/model_mixins/table_builder_class_methods.rb', line 3

def prepare_settings(logged_user, object, settings, params, per_page = 10)
  params[:page] = 1 if params[:page].blank?
  params[:order_by] = settings[:default][:order_by] + " " + settings[:default][:order_by_direction] if params[:order_by].blank? && !settings[:default][:order_by].blank? && !settings[:default][:order_by_direction].blank?

  params[:order_by] = settings[:default][:order] if params[:order_by].blank? && !settings[:default][:order].blank?


  params[:per_page] = per_page

  # method below can change this if there were some virtual non exixtent columns
  params[:real_order_by] = params[:order_by]
  check_non_existing_colum_order_by(settings, params)

  not_selected_items = object.filter(settings, params, per_page)
  items = not_selected_items.selection(settings)
  if params[:page].to_i > items.total_pages && items.total_pages > 0
    params[:page] = 1
    not_selected_items = object.filter(settings, params, per_page)
    items = not_selected_items.selection(settings)
  end

  if settings[:template].blank?
    another_global_formats = []
    another_formats = []
    column_methods = []
    settings[:columns].each do |col|
      unless col[:global_format_method].blank?
        # ToDo dodelat moznost predani parametru do formatovaci metody
        col[:name] = col[:name].blank? ? "non_existing_column___" + col[:global_format_method] : col[:name]
        another_global_format = {:global_format_method => col[:global_format_method],
                                 :name => col[:name],
                                 :table => col[:table]}
        another_global_formats << another_global_format
      end
      unless col[:format_method].blank?
        col[:name] = col[:name].blank? ? "non_existing_column___" + col[:format_method] : col[:name]
        another_format = {:format_method => col[:format_method],
                          :name => col[:name],
                          :table => col[:table]}
        another_formats << another_format
      end
      unless col[:column_method].blank?
        column_methods << {:column_method => col[:column_method],
                           :name => col[:name],
                           :table => col[:table],
                           :column_class => col[:column_class],
                           :column_params => col[:column_params]
        }
      end
    end


    all_items = items.all # maybe can be done more optimal
                          # same as template_items below, loads objects so column method are better to use
                          # todo think about, but I dont need object, because it's making the same query twice, I just need class and with one outer join it return filtered data, and i include includes to it
                          #template_items = object.joins("RIGHT OUTER JOIN (" + not_selected_items.select(settings[:row][:id] + " AS row_id").to_sql + ") temp_template_query ON #{settings[:row][:id]} = temp_template_query.row_id")
    if object.respond_to?(:klass)
      template_items = object.klass.joins("RIGHT OUTER JOIN (" + items.uniq.to_sql + ") temp_template_query ON #{settings[:row][:id]} = temp_template_query.row_id")
    else
      template_items = object.joins("RIGHT OUTER JOIN (" + items.uniq.to_sql + ") temp_template_query ON #{settings[:row][:id]} = temp_template_query.row_id")
    end

    template_items = template_items.includes(settings[:includes])

    #template_items.all
    # todo dat do knowledge base, kdyz chci aby fungoval include nesmim volat all
    #template_items.each {|t| t.meeting_registrations.each {|x| puts x.inspect}}

    another_columns = {}
    unless column_methods.blank?
      column_method_settings = {:params => params}
      column_methods.each do |column_method|
        column_method_settings[:column_params] = column_method[:column_params]
        #all items == array of array items
        #template items == AREL
        if column_method[:column_class].blank?
          if object.respond_to?(:klass)
            another_columns[column_method[:column_method]] = object.klass.send(column_method[:column_method], logged_user, all_items, template_items, column_method_settings)
          else
            another_columns[column_method[:column_method]] = object.send(column_method[:column_method], logged_user, all_items, template_items, column_method_settings)
          end
        else
          column_method[:column_class] = column_method[:column_class].constantize if column_method[:column_class].kind_of?(String)
          another_columns[column_method[:column_method]] = column_method[:column_class].send(column_method[:column_method], logged_user, all_items, template_items, column_method_settings)
        end
      end
    end

    if another_global_formats.blank? && another_formats.blank? && column_methods.blank?
      items_array = items
    else
      items_array = []
      all_items.each do |i|
        attrs = i.attributes
        another_global_formats.each do |another_global_format|
          # todo udelat moznost predani dalsich parametru
          attrs.merge!({"#{another_global_format[:table]}_#{another_global_format[:name]}" => i.send(another_global_format[:global_format_method].to_sym, attrs["#{another_global_format[:table]}_#{another_global_format[:name]}"])})
        end
        another_formats.each do |another_format|
          attrs.merge!({"#{another_format[:table]}_#{another_format[:name]}" => i.send(another_format[:format_method].to_sym, attrs["#{another_format[:table]}_#{another_format[:name]}"])})
        end
        column_methods.each do |column_method|
          another_column_row = "-"
          another_column_row = another_columns[column_method[:column_method]][attrs['row_id']] if !another_columns.blank? && !another_columns[column_method[:column_method]].blank? && !another_columns[column_method[:column_method]][attrs['row_id']].blank?
          attrs.merge!({"#{column_method[:table]}_#{column_method[:name]}" => another_column_row})
        end

        items_array << attrs
      end
    end

    settings.merge!({:data => items_array})
  else
    #template_items = object.joins("RIGHT OUTER JOIN (" + not_selected_items.uniq.select(settings[:row][:id] + " AS row_id").to_sql + ") temp_template_query ON #{settings[:row][:id]} = temp_template_query.row_id")
    if object.respond_to?(:klass)
      template_items = object.klass.joins("RIGHT OUTER JOIN (" + items.uniq.to_sql + ") temp_template_query ON #{settings[:row][:id]} = temp_template_query.row_id")
    else
      template_items = object.joins("RIGHT OUTER JOIN (" + items.uniq.to_sql + ") temp_template_query ON #{settings[:row][:id]} = temp_template_query.row_id")
    end

    template_items = template_items.includes(settings[:includes])
    settings.merge!({:data => template_items})
  end
  settings.merge!({:data_paginate => items})
  settings.merge!({:params => params})
  settings
end

#selection(settings) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/model_mixins/table_builder_class_methods.rb', line 131

def selection(settings)
  select_string = ""
  settings[:columns].each do |col|
    col[:table] = "unknown" if col[:table].blank?
    if col[:column_method].blank? && col[:row_method].blank? && !col[:name].blank?
      select_string += ", " unless select_string.blank?
      select_string += "#{col[:table]}.#{col[:name]} AS #{col[:table]}_#{col[:name]}"
    end

    # for select more data in combination with filter_method (etc full_name of user))
    if !col[:table].blank? && !col[:select].blank? && !col[:select_as].blank?
      col[:select_as] = col[:table] if col[:select_as].blank?

      col[:select].split(",").each do |one_select|
        one_select.gsub!(" ", "")
        select_string += ", " unless select_string.blank?
        select_string += "#{col[:select_as]}.#{one_select} AS #{col[:select_as]}_#{one_select}"
      end
    end

    # for (agregated data)
    #if (!col[:select_agregated].blank?
  end

  select_string += ", " unless select_string.blank?
  select_string += "#{settings[:row][:id]} AS row_id "

  select(select_string)
end