Class: Carload::Dashboard

Inherits:
Object
  • Object
show all
Defined in:
lib/carload/dashboard.rb

Direct Known Subclasses

Dashboard

Class Method Summary collapse

Class Method Details

.associate(options) ⇒ Object



17
18
19
20
21
22
# File 'lib/carload/dashboard.rb', line 17

def associate options
  model_a = options.keys.first
  model_b = options.values.first
  options.shift
  @@models[model_a].associations[model_b] = options
end

.default_modelObject



28
29
30
31
32
33
34
# File 'lib/carload/dashboard.rb', line 28

def default_model
  return @@default_model if defined? @@default_model
  @@models.each do |name, spec|
    return @@default_model = name if spec.default
  end
  @@default_model = @@models.keys.first
end

.model(name, &block) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/carload/dashboard.rb', line 4

def model name, &block
  name = name.to_sym
  if block_given?
    @@models ||= {}
    spec = @@models[name] || ModelSpec.new
    spec.name = name
    yield spec
    @@models[name] = spec
  else
    @@models[name]
  end
end

.modelsObject



24
25
26
# File 'lib/carload/dashboard.rb', line 24

def models
  @@models ||= {}
end

.write(file_path) ⇒ Object



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
# File 'lib/carload/dashboard.rb', line 36

def write file_path
  content = File.read("#{Carload::Engine.root}/lib/generators/carload/templates/dashboard.rb")
  content.gsub!(/^end$/, '')
  default = true
  models.each do |name, spec|
    content << "  model :\#{name} do |spec|\n    spec.default = \#{default}\n    spec.attributes.permitted = \#{spec.attributes.permitted}\n    spec.index_page.shows.attributes = \#{spec.index_page.shows.attributes}\n    spec.index_page.searches.attributes = \#{spec.index_page.searches.attributes}\n  end\n    RUBY\n    default = false\n    next if spec.associations.empty?\n    spec.associations.each_value do |association|\n      next unless association[:choose_by]\n      reflection = association[:reflection]\n      content << <<-RUBY\n  associate(\#{{ name.to_sym => reflection.name, choose_by: association[:choose_by] }})\n      RUBY\n    end\n  end\n  content << \"end\\n\"\n  File.open('app/carload/dashboard.rb', 'w') do |file|\n    file.write content\n    file.close\n  end\nend\n"