Class: ModelController

Inherits:
Object show all
Includes:
ReactiveAccessors
Defined in:
lib/volt/controllers/model_controller.rb

Direct Known Subclasses

Volt::NoticesController

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ReactiveAccessors

included

Constructor Details

#initialize(*args) ⇒ ModelController

Returns a new instance of ModelController.



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/volt/controllers/model_controller.rb', line 54

def initialize(*args)
  if args[0]
    # Assign the first passed in argument to attrs
    self.attrs = args[0]

    # If a model attribute is passed in, we assign it directly
    if attrs.respond_to?(:model)
      self.model = attrs.locals[:model]
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



111
112
113
# File 'lib/volt/controllers/model_controller.rb', line 111

def method_missing(method_name, *args, &block)
  return model.send(method_name, *args, &block)
end

Instance Attribute Details

#attrsObject

Returns the value of attribute attrs.



52
53
54
# File 'lib/volt/controllers/model_controller.rb', line 52

def attrs
  @attrs
end

Class Method Details

.model(val) ⇒ Object



8
9
10
# File 'lib/volt/controllers/model_controller.rb', line 8

def self.model(val)
  @default_model = val
end

.new(*args, &block) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/volt/controllers/model_controller.rb', line 42

def self.new(*args, &block)
  inst = self.allocate

  inst.model = (@default_model || :controller)

  inst.initialize(*args, &block)

  return inst
end

Instance Method Details

#channelObject



99
100
101
# File 'lib/volt/controllers/model_controller.rb', line 99

def channel
  $page.channel
end

#controllerObject



107
108
109
# File 'lib/volt/controllers/model_controller.rb', line 107

def controller
  @controller ||= Model.new
end

#flashObject



83
84
85
# File 'lib/volt/controllers/model_controller.rb', line 83

def flash
  $page.flash
end

#go(url) ⇒ Object

Change the url params, similar to redirecting to a new url



67
68
69
# File 'lib/volt/controllers/model_controller.rb', line 67

def go(url)
  self.url.parse(url)
end

#local_storeObject



91
92
93
# File 'lib/volt/controllers/model_controller.rb', line 91

def local_store
  $page.local_store
end

#modelObject



31
32
33
34
35
36
37
38
39
40
# File 'lib/volt/controllers/model_controller.rb', line 31

def model
  model = self.current_model

  # If the model is a proc, call it now
  if model.is_a?(Proc)
    model = model.call
  end

  return model
end

#model=(val) ⇒ Object

Sets the current model on this controller



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/volt/controllers/model_controller.rb', line 13

def model=(val)
  # Start with a nil reactive value.
  self.current_model ||= Model.new

  if Symbol === val || String === val
    collections = [:page, :store, :params, :controller]
    if collections.include?(val.to_sym)
      self.current_model = self.send(val)
    else
      raise "#{val} is not the name of a valid model, choose from: #{collections.join(', ')}"
    end
  elsif val
    self.current_model = val
  else
    raise "model can not be #{val.inspect}"
  end
end

#pageObject



71
72
73
# File 'lib/volt/controllers/model_controller.rb', line 71

def page
  $page.page
end

#pagedObject



75
76
77
# File 'lib/volt/controllers/model_controller.rb', line 75

def paged
  $page.page
end

#paramsObject



87
88
89
# File 'lib/volt/controllers/model_controller.rb', line 87

def params
  $page.params
end

#storeObject



79
80
81
# File 'lib/volt/controllers/model_controller.rb', line 79

def store
  $page.store
end

#tasksObject



103
104
105
# File 'lib/volt/controllers/model_controller.rb', line 103

def tasks
  $page.tasks
end

#urlObject



95
96
97
# File 'lib/volt/controllers/model_controller.rb', line 95

def url
  $page.url
end