Module: MegaScaffold::Routing

Defined in:
lib/mega_scaffold/routing.rb

Instance Method Summary collapse

Instance Method Details

#mega_scaffold(*options, fields: nil, ignore: [:id, :created_at, :updated_at], except: [], only: [], collection: nil, parent: nil, concerns: nil, layout: "application") ⇒ Object



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
# File 'lib/mega_scaffold/routing.rb', line 4

def mega_scaffold(*options,
    fields: nil,
    ignore: [:id, :created_at, :updated_at],
    except: [],
    only: [],
    collection: nil,
    parent: nil,
    concerns: nil,
    layout: "application"
  )

  model_name  = options[0].to_s.singularize # user
  model       = model_name.classify.safe_constantize # User
  concerns    = Array.wrap(concerns)
  return unless model

  fields    = MegaScaffold::FieldsGenerator.new(model).generate if fields.blank?
  generator = MegaScaffold::CodeGenerator.new({
    namespaces: @scope[:module].to_s.split("/").map{|e| e.classify},
    concerns: concerns,
    model: model,
    scope: @scope,
    layout: layout
  })

  klass = eval(generator.generate)

  MegaScaffold::KlassDecorator.new(klass, {
    fields: fields,
    collection: collection,
    parent: parent,
    model: model,
    except: except,
    only: only,
    ignore: ignore
  }).decorate

  resources *options
rescue ActiveRecord::StatementInvalid => ex
  puts ex.message
end