Module: MegaBar::MegaBarConcern

Instance Method Summary collapse

Instance Method Details

#add_form_path_to_mega_displays(mega_env) ⇒ Object



150
151
152
153
154
155
# File 'app/controllers/mega_bar/mega_bar_concern.rb', line 150

def add_form_path_to_mega_displays(mega_env)
  mega_env[:mega_displays].each_with_index do | mega_display, index |
    mega_env[:mega_displays][index][:form_path] = form_path(params[:action], mega_env[:kontroller_path], params[:id])
  end
  mega_env
end

#collect_filters(filter_types) ⇒ Object



239
240
241
242
243
244
245
246
247
248
249
250
# File 'app/controllers/mega_bar/mega_bar_concern.rb', line 239

def collect_filters(filter_types)
  filters = Hash[filter_types.map {|v| [v, []] }]
  params[@kontroller_inst].each do |key, value|
    @mega_displays.each do |md|
      md[:displayable_fields].each do |df|
        filters[ df[:field].filter_type] <<  { df[:field].field => value } if !df[:field].filter_type.blank? && key.sub('___filter', '') == df[:field].field
        # @mega_displays[0][:displayable_fields][0][:field].filter_type
      end
    end
  end
  filters
end

#column_sortingObject



261
262
263
# File 'app/controllers/mega_bar/mega_bar_concern.rb', line 261

def column_sorting
  sort_column(@mega_class, @mega_model_properties, params) + " " + sort_direction(params, @mega_model_properties)
end

#conditionsObject



147
148
149
# File 'app/controllers/mega_bar/mega_bar_concern.rb', line 147

def conditions
   @conditions.merge!(env[:mega_env][:nested_ids][0]) if env[:mega_env][:nested_ids][0]
end

#constant_from_controller(str) ⇒ Object



252
253
254
255
256
257
258
259
260
# File 'app/controllers/mega_bar/mega_bar_concern.rb', line 252

def constant_from_controller(str)
  logger.info("str::::" + str)
  constant_string = ''
  str.split('/').each_with_index do | seg, i |
    constant_string +=  i < str.split('/').size - 1 ? seg.classify + '::' : seg.classify
  end
  logger.info("constant string" + constant_string)
  constant_string
end

#createObject



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
# File 'app/controllers/mega_bar/mega_bar_concern.rb', line 40

def create
  @mega_instance = @mega_class.new(_params)
  respond_to do |format|
    if @mega_instance.save
      MegaBar.call_rake('db:schema:dump') if [1,2].include? @model_id and Rails.env != 'test'# gets new models into schema
      param_hash = {}
      @nested_ids.each do |param|
        param_hash = param_hash.merge(param)
      end
      param_hash[:controller] = params["controller"]
      if @mega_instance.id
        # todo: add configuration to model_display for where to go after insert.
        param_hash[:id] = @mega_instance.id if @mega_instance.id #danger.. added during testing.
        param_hash[:action] = 'show'
      else
        param_hash[:action] = 'index'
      end
      param_hash[:only_path] = true
      format.html { redirect_to url_for(param_hash), notice: 'It was successfully created.' }
      format.json { render action: 'show', status: :created, location: @mega_instance }
    else
      format.html {
        redo_setup('new')
        render @new_view_template
       }
      format.json { render json: @model.errors, status: :unprocessable_entity }
    end
  end
end

#destroyObject



87
88
89
90
91
92
93
94
95
96
97
# File 'app/controllers/mega_bar/mega_bar_concern.rb', line 87

def destroy
  instance_variable_set("@" + @kontroller_inst,  @mega_class.find(params[:id]))
  @mega_instance = instance_variable_get("@" + @kontroller_inst);
  session[:return_to] ||= request.referer

  @mega_instance.destroy
  respond_to do |format|
    format.html {  redirect_to session.delete(:return_to), notice: 'You nuked it properly.' }
    format.json { head :no_content }
  end
end

#editObject



29
30
31
32
33
34
35
# File 'app/controllers/mega_bar/mega_bar_concern.rb', line 29

def edit
  session[:return_to] = request.referer
  instance_variable_set("@"  + @kontroller_inst,  @mega_class.find(params[:id]))
  @mega_instance = instance_variable_get("@"  + @kontroller_inst)
  @form_instance_vars = @nested_instance_variables  + [@mega_instance]
  render @edit_view_template
end

#form_path(action, path, id = nil) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'app/controllers/mega_bar/mega_bar_concern.rb', line 157

def form_path(action, path, id=nil)
  #used on index and show forms (for filters & reordering)
  param_hash = {}
  @nested_ids.each do |param|
    param_hash = param_hash.merge(param)
  end
  param_hash = param_hash.merge(params.dup)
  param_hash[:id] = id
  param_hash[:only_path] = true

  case action
  when 'new'
    param_hash['action'] = 'create'
  when 'edit'
    param_hash['action'] = 'update'
  else
     param_hash['action'] = action
  end
  url_for(param_hash)
end

#indexObject



5
6
7
8
9
10
11
12
# File 'app/controllers/mega_bar/mega_bar_concern.rb', line 5

def index
  records = @mega_class.where(@conditions).where(@conditions_array).order(column_sorting)
  instance_variable_set("@" + @kontroller_inst.pluralize,  records)
  @mega_instance ||= instance_variable_get("@" + @kontroller_inst.pluralize);
  @mega_instance = @mega_instance.page(@page_number).per(num_per_page) if might_paginate?
  @mega_instance = process_filters(@mega_instance)
  render @index_view_template
end

#is_displayable?(format) ⇒ Boolean

Returns:

  • (Boolean)


185
186
187
# File 'app/controllers/mega_bar/mega_bar_concern.rb', line 185

def is_displayable?(format)
  return  (format == 'hidden' || format == 'off') ? false : true
end

#might_filter?(location = nil) ⇒ Boolean

Returns:

  • (Boolean)


197
198
199
200
201
202
203
# File 'app/controllers/mega_bar/mega_bar_concern.rb', line 197

def might_filter?(location = nil)
  if (location)
    (@mega_displays[0].dig(:collection_settings)&.pagination_position == location || @mega_displays[0].dig(:collection_settings)&.pagination_position == 'both') && !@mega_instance.blank?
  else
    @mega_displays[0].dig(:collection_settings)&.filter_fields && !@mega_instance.blank?
  end
end

#might_paginate?(location = nil) ⇒ Boolean

Returns:

  • (Boolean)


189
190
191
192
193
194
195
# File 'app/controllers/mega_bar/mega_bar_concern.rb', line 189

def might_paginate?(location = nil)
  if (location)
    (@mega_displays[0].dig(:collection_settings)&.pagination_position == location || @mega_displays[0].dig(:collection_settings)&.pagination_position == 'both') && !@mega_instance.blank?
  else
    !@mega_displays[0].dig(:collection_settings)&.pagination_position.blank? && !@mega_instance.blank?
  end
end

#moveObject



275
276
277
278
279
280
281
282
283
# File 'app/controllers/mega_bar/mega_bar_concern.rb', line 275

def move
  if ["move_lower", "move_higher", "move_to_top", "move_to_bottom"].include?(params[:method]) and @mega_rout[:id] =~ /^\d+$/
    @mega_class.find(@mega_rout[:id]).send(params[:method])
  end
  session[:return_to] ||= request.referer
  respond_to do |format|
    format.html { redirect_to session.delete(:return_to), notice: 'Thing was successfully mooved.' }
  end
end

#newObject



21
22
23
24
25
26
27
# File 'app/controllers/mega_bar/mega_bar_concern.rb', line 21

def new
  session[:return_to] = request.referer
  instance_variable_set("@"  + @kontroller_inst,  @mega_class.new)
  @mega_instance = instance_variable_get("@"  + @kontroller_inst);
  @form_instance_vars = @nested_instance_variables  + [@mega_instance]
  render @new_view_template
end

#num_per_pageObject



204
205
206
# File 'app/controllers/mega_bar/mega_bar_concern.rb', line 204

def num_per_page
  @mega_displays[0].dig(:collection_settings)&.records_per_page.blank? ? 6  : @mega_displays[0].dig(:collection_settings)&.records_per_page
end

#process_filters(mega_instance) ⇒ Object



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
# File 'app/controllers/mega_bar/mega_bar_concern.rb', line 208

def process_filters(mega_instance)
  if params["commit"] == "clear_filters"
    session[:mega_filters] = {}
  end
  session[:mega_filters] ||= {}
  return mega_instance unless params[@kontroller_inst] || session[:mega_filters][@kontroller_inst]
  #cache me.
  if params[@kontroller_inst]
    session[:mega_filters] = {}
    filter_types = MegaBar::Field.includes(:options).find_by(field: 'filter_type', tablename: 'mega_bar_fields').options.pluck(:value)
    filters = session[:mega_filters][@kontroller_inst.to_sym] = collect_filters(filter_types)
  elsif session[:mega_filters][@kontroller_inst]
     filters = session[:mega_filters][@kontroller_inst]
  end
  return mega_instance if filters.blank?
  @filter_text = []
  filters.each do |key, filt|
    case key
    when 'contains'
      filt.each do | hsh |
        hsh.each do | field, value |
          @filter_text << "#{field} contains #{value}" unless value.blank?
          mega_instance = mega_instance.where(field + " like ?", "%" + value + "%")
        end
      end
    end
    # $50 bounty for each additional case.
  end
  mega_instance
end

#redo_setup(action) ⇒ Object



264
265
266
267
268
269
270
271
272
273
# File 'app/controllers/mega_bar/mega_bar_concern.rb', line 264

def redo_setup(action)
  env[:mega_rout][:action] = action
  env[:mega_env] = MegaEnv.new(@block, env[:mega_rout], env[:mega_page], []).to_hash
  set_vars_for_all
  set_vars_for_displays
  params[:action] = 'edit'
  params[:redo] = true
  @form_instance_vars = @nested_instance_variables  + [@mega_instance]
  'hello'
end

#set_vars_for_allObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'app/controllers/mega_bar/mega_bar_concern.rb', line 108

def set_vars_for_all
  @mega_page = env[:mega_page]
  @mega_rout = env[:mega_rout]
  @mega_layout = env[:mega_layout]
  @mega_class = env[:mega_env][:klass]
  @mega_layout_section = env[:mega_layout_section]
  @block_class =  env['block_class']
  env[:mega_env].keys.each { | env_var | instance_variable_set('@' + env_var.to_s, env[:mega_env][env_var]) }
  # that line sets these instance vars that were determined in the layout_engine middleware
  # @block,
  # @modle_id,
  # @mega_model_properties
  # @klass
  # @kontroller_inst
  # @kontroller_path
  # @mega_displays (an arry of stuff like model_display_format, :displayable_fields, :model_display, :collection_settings, :form_path )
  # @nested_ids
  # @nested_class_info,
  # @page_number
  unpack_nested_classes(@nested_class_info)
  @index_view_template ||= "mega_bar.html.erb"
  @show_view_template ||= "mega_bar.html.erb"
  @edit_view_template ||= "mega_bar.html.erb"
  @new_view_template ||= "mega_bar.html.erb"
  session[:mega_filters] ||= {}
end

#set_vars_for_displaysObject



99
100
101
102
103
104
105
106
# File 'app/controllers/mega_bar/mega_bar_concern.rb', line 99

def set_vars_for_displays
  @conditions =  {}; self.try(:conditions) #allows various 'where' statements in queries.
  @conditions_array =  []; self.try(:conditions_array)
  @options = {}; self.try(:get_options) # allows for queries to populate menus
  env[:mega_env] = add_form_path_to_mega_displays(env[:mega_env])
  @default_options = {}
  @mega_displays = env[:mega_env][:mega_displays]
end

#showObject



14
15
16
17
18
19
# File 'app/controllers/mega_bar/mega_bar_concern.rb', line 14

def show
  @mega_instance ||= []
  instance_variable_set("@"  + @kontroller_inst,  @mega_class.find(params[:id]))
  @mega_instance << instance_variable_get("@"  + @kontroller_inst);
  render @show_view_template
end

#sort_column(mega_class, model_properties, passed_params) ⇒ Object



178
179
180
# File 'app/controllers/mega_bar/mega_bar_concern.rb', line 178

def sort_column(mega_class, model_properties, passed_params)
  mega_class.column_names.include?(passed_params[:sort]) ? passed_params[:sort] :  model_properties[:default_sort_field]
end

#sort_direction(passed_params, model_properties) ⇒ Object



181
182
183
# File 'app/controllers/mega_bar/mega_bar_concern.rb', line 181

def sort_direction(passed_params, model_properties)
  %w[asc desc].include?(passed_params[:direction]) ? passed_params[:direction] :  model_properties[:default_sort_order]
end

#test_createObject

$20 bounty for understanding why this is here and figuring out how to get rid of it! (hint: tests break without it)



37
38
39
# File 'app/controllers/mega_bar/mega_bar_concern.rb', line 37

def test_create # $20 bounty for understanding why this is here and figuring out how to get rid of it! (hint: tests break without it)
   create
end

#unpack_nested_classes(nested_class_infos) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
# File 'app/controllers/mega_bar/mega_bar_concern.rb', line 135

def unpack_nested_classes(nested_class_infos)
  # nested as in nested resource routes.
  nested_instance_variables = []
  nested_class_infos.each_with_index do |info, idx|
    # puts 'make a instance var!'
    if @nested_ids[idx]
      instance_variable_set("@" + info[1], info[0].constantize.find(@nested_ids[idx].map{|k,v|v}).first)
      nested_instance_variables << instance_variable_get("@" + info[1])
    end
  end
  @nested_instance_variables = nested_instance_variables.reverse
end

#updateObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'app/controllers/mega_bar/mega_bar_concern.rb', line 70

def update
  instance_variable_set("@" + @kontroller_inst, @mega_class.find(params[:id]))
  @mega_instance = instance_variable_get("@" + @kontroller_inst)
  respond_to do |format|
    if @mega_instance.update(_params)
      session[:return_to] ||= request.referer
      format.html { redirect_to session.delete(:return_to), notice: 'Thing was successfully updated.' }
      format.json { respond_with_bip(@mega_instance) }
    else
      format.html {
        redo_setup('edit')
        render action: 'mega_bar.html.erb'
      }
      format.json { render json: @mega_instance.errors, status: :unprocessable_entity }
    end
  end
end