Class: ModelController

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

Direct Known Subclasses

Volt::NoticesController

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ReactiveAccessors

included

Constructor Details

#initialize(*args) ⇒ ModelController

Returns a new instance of ModelController.



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

def initialize(*args)


  # Set the instance variable to match any passed in arguments
  if args.size > 0
    args[0].each_pair do |key, value|
      instance_variable_set(:"@#{key}", value)
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



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

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

Class Method Details

.model(val) ⇒ Object



6
7
8
# File 'lib/volt/controllers/model_controller.rb', line 6

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

.new(*args, &block) ⇒ Object



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

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

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

  inst.initialize(*args, &block)

  return inst
end

Instance Method Details

#channelObject



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

def channel
  $page.channel
end

#controllerObject



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

def controller
  @controller ||= ReactiveValue.new(Model.new)
end

#flashObject



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

def flash
  $page.flash
end

#go(url) ⇒ Object

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



55
56
57
# File 'lib/volt/controllers/model_controller.rb', line 55

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

#modelObject



29
30
31
# File 'lib/volt/controllers/model_controller.rb', line 29

def model
  @model
end

#model=(val) ⇒ Object

Sets the current model on this controller



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

def model=(val)
  # Start with a nil reactive value.
  @model ||= ReactiveValue.new(Proc.new { nil })

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

#pageObject



59
60
61
# File 'lib/volt/controllers/model_controller.rb', line 59

def page
  $page.page
end

#pagedObject



63
64
65
# File 'lib/volt/controllers/model_controller.rb', line 63

def paged
  $page.page
end

#paramsObject



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

def params
  $page.params
end

#storeObject



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

def store
  $page.store
end

#tasksObject



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

def tasks
  $page.tasks
end

#urlObject



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

def url
  $page.url
end