Class: Volt::ModelController

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

Direct Known Subclasses

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.



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

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



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

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

Instance Attribute Details

#attrsObject

Returns the value of attribute attrs.



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

def attrs
  @attrs
end

Class Method Details

.model(val) ⇒ Object



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

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

.new(*args, &block) ⇒ Object



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

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

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

  inst.initialize(*args, &block)

  inst
end

Instance Method Details

#channelObject



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

def channel
  $page.channel
end

#controllerObject



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

def controller
  @controller ||= Model.new
end

#flashObject



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

def flash
  $page.flash
end

#go(url) ⇒ Object

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



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

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

#local_storeObject



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

def local_store
  $page.local_store
end

#modelObject



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

def model
  model = self.current_model

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

  model
end

#model=(val) ⇒ Object

Sets the current model on this controller



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

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 = send(val)
    else
      fail "#{val} is not the name of a valid model, choose from: #{collections.join(', ')}"
    end
  elsif val
    self.current_model = val
  else
    fail "model can not be #{val.inspect}"
  end
end

#pageObject



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

def page
  $page.page
end

#pagedObject



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

def paged
  $page.page
end

#paramsObject



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

def params
  $page.params
end

#storeObject



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

def store
  $page.store
end

#tasksObject



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

def tasks
  $page.tasks
end

#urlObject



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

def url
  $page.url
end