Class: MegaRoute

Inherits:
Object
  • Object
show all
Defined in:
lib/mega_bar/mega_route.rb

Class Method Summary collapse

Class Method Details

.boopObject



129
130
131
# File 'lib/mega_bar/mega_route.rb', line 129

def self.boop
  abort('booop')
end

.controller_from_block(context, block) ⇒ Object



3
4
5
6
7
8
9
10
11
# File 'lib/mega_bar/mega_route.rb', line 3

def self.controller_from_block(context, block)
  md = MegaBar::ModelDisplay.by_block(block.id).first
  m = MegaBar::Model.find(md.model_id)
  return unless m
  modu = m.modyule.empty? ? '' : m.modyule + '::'
  # modu == 'MegaBar::' ?  'mega-bar/' + m.classname.tableize
  # puts md.model_id.to_s + " .... " + m.id.to_s + " = " + m.classname.tableize
  m.classname.tableize
end

.load(context) ⇒ Object



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
# File 'lib/mega_bar/mega_route.rb', line 13

def self.load(context)
  # puts 'context: ' + context.inspect
  routes = []
  ases = []
  MegaBar::Page.all.each do |pg|
    # puts 'new page '
    MegaBar::Layout.by_page(pg.id).each do |llayout|
      # puts 'new layout'
      llayout.layout_sections.each do | layout_section |
        MegaBar::Block.by_layout_section(layout_section.id).each do | block |
          # byebug if pg.id  == 18 || pg.id == 33
          p = block.path_base? ? block.path_base : pg.path
          if context.kind_of?(Array)
            exclude = false
            context.each do |c|
              exclude = true if p.starts_with? c
            end
            next if exclude
          else
            if p.starts_with? context
              p = p[context.size..-1]
            else
              next
            end
          end
          path = ''
          p1 = p.split('/')
          p1.each do | seg |
            next if (seg.empty? || seg.starts_with?(':'))
            path += seg.singularize + '_'
          end
          path = path.chomp('_').pluralize
          # puts "new block: " + block.id.to_s
          # byebug if  block.id == 74
          # puts "path: " + path
          # byebug if pg.id > 29 || pg.id == 13 || pg.id == 10
          if block.html?
            p = block.path_base? ? block.path_base : pg.path
            routes << {path: p, method: 'get', controller: 'flats', action: 'index', as: 'flats_' + block.id.to_s}
            # puts 'block html path. ' + p.to_s
          else
            if MegaBar::ModelDisplay.by_block(block.id).size > 0
             # byebug if pg.id ==10
              # byebug if MegaBar::ModelDisplay.by_block(block.id).first.model_id == 3
              begin
                controller = MegaRoute.controller_from_block(context, block)
              rescue
              next
              end
              # puts "controller ---- " + controller + ", path: " + p
              MegaBar::ModelDisplay.by_block(block.id).order(collection_or_member: :asc, action: :asc).each do | md | #order here becomes important todo
                # puts "mid #{block.name}" + md.action.to_s
                modle = MegaBar::Model.find(md.model_id)
                pf = ''
                as = nil
                concerns = nil
                case md.action
                when 'show'
                  pf = p + '/:id'
                  as = path.singularize
                  meth = 'get'
                when 'index'
                  pf = p
                  as = path
                  # byebug if as == 'templates'
                  concerns = 'paginatable'
                  meth = [:get]
                when 'new'
                  pf = p + '/new'
                  as = 'new_' + path
                  meth = 'get'
                when 'edit'
                  pf = p + '/:id/edit'
                  as = 'edit_' + path
                  meth = 'get'
                else
                  pf = p.to_s + "/" + md.action.to_s
                  if md.collection_or_member == 'collection'
                    concerns = 'paginatable'
                    meth =  [:get, :post]
                  end
                  # puts 'custom action: ' + pf
                  # db should track whether custom model_display actions are on member or collection and if they have a special 'as' or anything.
                end
                route = {path: pf, method: meth, action: md.action, controller: controller}
                route = route.merge({as: as}) if as
                # byebug if as == 'templates'
                route = route.merge({concerns: concerns}) if concerns
                # route = route.merge({on: x}) if x
                routes << route
                routes << {path: pf + '/filter', method: [:get, :post], action: md.action, controller: controller} if md.collection_or_member == 'collection'
                # if md.collection_or_member == 'collection'
                #   c_route = route
                #   c_route[:method] = 'post'
                #   routes << c_route
                # end

              end
              routes << {path: p + '/move/:id', method: 'get', action: 'move', controller: controller }
              routes << {path: p + '/:id', method: 'patch', action: 'update', controller: controller}
              routes << {path: p, method: 'post', action: 'create', controller: controller}
              routes << {path: p + '/:id', method: 'delete', action: 'destroy', controller: controller}
            end
          end
        end  #blocks each
      end ###layout sections each
    end
  end #pages each
  routes.uniq
rescue ActiveRecord::StatementInvalid # you can also add this
  []
end

.reloadObject



126
127
128
# File 'lib/mega_bar/mega_route.rb', line 126

def self.reload
  ComingSoon::Application.routes_reloader.reload!
end