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
# 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



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

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



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

def channel
  $page.channel
end

#controllerObject



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

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

#flashObject



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

def flash
  $page.flash
end

#go(url) ⇒ Object

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



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

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



57
58
59
# File 'lib/volt/controllers/model_controller.rb', line 57

def page
  $page.page
end

#pagedObject



61
62
63
# File 'lib/volt/controllers/model_controller.rb', line 61

def paged
  $page.page
end

#paramsObject



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

def params
  $page.params
end

#storeObject



65
66
67
# File 'lib/volt/controllers/model_controller.rb', line 65

def store
  $page.store
end

#tasksObject



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

def tasks
  $page.tasks
end

#urlObject



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

def url
  $page.url
end