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.



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

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



119
120
121
122
123
124
125
126
127
# File 'lib/volt/controllers/model_controller.rb', line 119

def method_missing(method_name, *args, &block)
  model = self.model

  if model
    model.send(method_name, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#attrsObject

Returns the value of attribute attrs.



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

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



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

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

  inst.model = @default_model if @default_model

  inst.initialize(*args, &block)

  inst
end

Instance Method Details

#channelObject



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

def channel
  $page.channel
end

#controllerObject



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

def controller
  @controller ||= Model.new
end

#flashObject



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

def flash
  $page.flash
end

#go(url) ⇒ Object

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



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

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

#loaded?Boolean

Returns:



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

def loaded?
  respond_to?(:state) && state == :loaded
end

#local_storeObject



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

def local_store
  $page.local_store
end

#modelObject



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

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
# 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
  else
    self.current_model = val
  end
end

#pageObject



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

def page
  $page.page
end

#paramsObject



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

def params
  $page.params
end

#respond_to?(method_name) ⇒ Boolean

Check if this controller responds_to method, or the model

Returns:



111
112
113
114
115
116
117
# File 'lib/volt/controllers/model_controller.rb', line 111

def respond_to?(method_name)
  super || begin
    model = self.model

    model.respond_to?(method_name) if model
  end
end

#storeObject



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

def store
  $page.store
end

#tasksObject



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

def tasks
  $page.tasks
end

#urlObject



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

def url
  $page.url
end