class MegaEnv

Attributes

block[R]
block_action[R]
klass[R]
kontroller_inst[R]
kontroller_klass[R]
kontroller_path[R]
mega_displays[RW]
mega_model_properties[RW]
modle[R]
modle_id[R]
nested_class_info[R]
nested_ids[RW]
params_hash_arr[R]

Public Class Methods

new(blck, rout, page_info, pagination) click to toggle source
# File lib/mega_bar/layout_engine.rb, line 230
def initialize(blck, rout, page_info, pagination)
  @block_model_displays =   MegaBar::ModelDisplay.by_block(blck.id)
  @displays = blck.actions == 'current' ? @block_model_displays.by_block(blck.id).by_action(rout[:action]) : @block_model_displays.by_block(blck.id)
  @block_action = @displays.empty? ? rout[:action] : @displays.first.action
  @modle = MegaBar::Model.by_model(@block_model_displays.first.model_id).first
  @modle_id = @modle.id
  @modyule = @modle.modyule.empty? ? '' : @modle.modyule + '::'
  @kontroller_klass = @modyule + @modle.classname.classify.pluralize + "Controller"
  @kontroller_path = @modle.modyule.nil? || @modle.modyule.empty? ?   @modle.classname.pluralize.underscore :  @modyule.split('::').map { | m | m = m.underscore }.join('/') + '/' + @modle.classname.pluralize.underscore
  @klass = (@modyule + @modle.classname.classify).constantize
  meta_programming(@klass, @modle)
  @kontroller_inst = @modle.classname.underscore
  @mega_displays = set_mega_displays(@displays)
  @nested_ids, @params_hash_arr, @nested_classes = nest_info(blck, rout, page_info)
  @nested_class_info = set_nested_class_info(@nested_classes)
  @block = blck
  @page_number = pagination.map {|info| info[:page].to_i if info[:kontrlr] == @kontroller_inst + '_page' }.compact.first
end

Public Instance Methods

is_displayable?(format) click to toggle source
# File lib/mega_bar/layout_engine.rb, line 338
def is_displayable?(format)
  return  (format == 'hidden' || format == 'off') ? false : true
end
meta_programming(klass, modle) click to toggle source
# File lib/mega_bar/layout_engine.rb, line 264
def meta_programming(klass, modle)
  position_parent_method = modle.position_parent.split("::").last.underscore.downcase.to_sym unless modle.position_parent.blank?
  klass.class_eval{ acts_as_list scope: position_parent_method, add_new_at: :bottom } if position_parent_method
end
nest_info(blck, rout, page_info) click to toggle source
# File lib/mega_bar/layout_engine.rb, line 298
def nest_info(blck, rout, page_info)
  params_hash_arr = []
  nested_ids = []
  nested_classes = []
  puts "================="
  puts blck, rout, page_info

  if blck.path_base
    if page_info[:page_path].starts_with?(blck.path_base) || blck.path_base.starts_with?(page_info[:page_path])
      block_path_vars = blck.path_base.split('/').map{ | m | m if m[0] == ':'} - ["", nil]
      depth = 0
      puts 'bpv' + block_path_vars.to_s
      until depth == block_path_vars.size + 1
        # puts MegaBar::Model.find(blck.send("nest_level_#{depth}"))
        blck_model = depth == 0 ? modle :  MegaBar::Model.find(blck.send("nest_level_#{depth}"))
        fk_field =  depth == 0 ? 'id' : blck_model.classname.underscore.downcase +  '_id'
        new_hash = {fk_field => page_info[:vars][block_path_vars.size - depth]}
        params_hash_arr <<  new_hash
        nested_ids << new_hash if depth > 0
        nested_classes << blck_model
        depth += 1
      end
    end
  else
    # you can do layouts with a block nested one deep without setting path_base
    params_hash_arr << h =(rout[:id] && blck.nest_level_1.nil?) ? {id: rout[:id]} : {id: nil}
    params_hash_arr << i =  {MegaBar::Model.find(blck.nest_level_1).classname.underscore + '_id' =>  rout[:id]} if !blck.nest_level_1.nil?
    nested_ids << i if i
  end
  return nested_ids, params_hash_arr, nested_classes
end
set_mega_displays(displays) click to toggle source
# File lib/mega_bar/layout_engine.rb, line 268
def set_mega_displays(displays)
  mega_displays_info = [] # collects model and field display settings
  displays.each do | display |
    model_display_format = MegaBar::ModelDisplayFormat.find(display.format)
    model_display_collection_settings = MegaBar::ModelDisplayCollection.by_model_display_id(display.id).first if display.collection_or_member == 'collection'
    field_displays = MegaBar::FieldDisplay.by_model_display_id(display.id).order('position asc')
    displayable_fields = []
    field_displays.each do |field_disp|
      field = MegaBar::Field.find(field_disp.field_id)
      if is_displayable?(field_disp.format)
        #lets figure out how to display it right here.
        puts field_disp.format
        data_format = Object.const_get('MegaBar::' + field_disp.format.classify).by_field_display_id(field_disp.id).last #data_display models have to have this scope!
        if field_disp.format == 'select'
          options = MegaBar::Option.where(field_id: field.id).collect {|o| [ o.text, o.value ] }
        end
        displayable_fields << {field_display: field_disp, field: field, data_format: data_format, options: options, obj: @mega_instance}
      end
    end
    info = {
      :model_display_format => model_display_format, # Object.const_get('MegaBar::' + MegaBar::RecordsFormat.find(md.format).name).new,
      :displayable_fields => displayable_fields,
      :model_display => display,
      :collection_settings => model_display_collection_settings
    }
    mega_displays_info << info
  end
  mega_displays_info
end
set_nested_class_info(nested_classes) click to toggle source
# File lib/mega_bar/layout_engine.rb, line 329
def set_nested_class_info(nested_classes)
  nested_class_info = []
  nested_classes.each_with_index do |nc, idx|
    modyule = nc.modyule.empty? ? '' : nc.modyule + '::'
    klass = modyule + nc.classname.classify
    nested_class_info << [klass, nc.classname.underscore] if idx != 0
  end
  nested_class_info
end
to_hash() click to toggle source
# File lib/mega_bar/layout_engine.rb, line 249
def to_hash
  {
    block: @block,
    modle_id: @modle_id,
    mega_model_properties: @modle,
    klass: @klass,
    kontroller_inst: @kontroller_inst,
    kontroller_path: @kontroller_path,
    mega_displays: @mega_displays,
    nested_ids: @nested_ids,
    nested_class_info: @nested_class_info,
    page_number: @page_number
  }
end