Class: Workarea::ApplicationViewModel

Inherits:
Object
  • Object
show all
Defined in:
app/view_models/workarea/application_view_model.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model = nil, options = {}) ⇒ ApplicationViewModel

Returns a new instance of ApplicationViewModel.



13
14
15
16
17
18
19
20
# File 'app/view_models/workarea/application_view_model.rb', line 13

def initialize(model = nil, options = {})
  @model = model
  @options = if options.respond_to?(:to_unsafe_h)
               options.to_unsafe_h.with_indifferent_access
             else
               options.to_h.with_indifferent_access
             end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/view_models/workarea/application_view_model.rb', line 22

def method_missing(method, *args, &block)
  if model && model.respond_to?(method)
    # Define a method so the next call is faster
    self.class.send(:define_method, method) do |*args, &blok|
      model.send(method, *args, &blok)
    end

    send(method, *args, &block)
  else
    super
  end

rescue NoMethodError => no_method_error
  super if no_method_error.name == method
  raise no_method_error
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



3
4
5
# File 'app/view_models/workarea/application_view_model.rb', line 3

def model
  @model
end

#optionsObject (readonly)

Returns the value of attribute options.



3
4
5
# File 'app/view_models/workarea/application_view_model.rb', line 3

def options
  @options
end

Class Method Details

.wrap(input, options = {}) ⇒ Object



5
6
7
8
9
10
11
# File 'app/view_models/workarea/application_view_model.rb', line 5

def self.wrap(input, options = {})
  if input.is_a?(Enumerable)
    input.map { |i| new(i, options) }
  else
    new(input, options)
  end
end

Instance Method Details

#==(other) ⇒ Object



63
64
65
# File 'app/view_models/workarea/application_view_model.rb', line 63

def ==(other)
  (other.class == self.class && other.id == id) || other == model
end

#blank?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'app/view_models/workarea/application_view_model.rb', line 55

def blank?
  model.blank?
end

#nil?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'app/view_models/workarea/application_view_model.rb', line 59

def nil?
  model.nil?
end

#present?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'app/view_models/workarea/application_view_model.rb', line 51

def present?
  model.present?
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
# File 'app/view_models/workarea/application_view_model.rb', line 39

def respond_to_missing?(method_name, include_private = false)
  super || (model && model.respond_to?(method_name))
end

#to_modelObject



47
48
49
# File 'app/view_models/workarea/application_view_model.rb', line 47

def to_model
  model
end

#to_paramObject



43
44
45
# File 'app/view_models/workarea/application_view_model.rb', line 43

def to_param
  model.to_param
end

#translate(key, options = {}) ⇒ Object Also known as: t



67
68
69
# File 'app/view_models/workarea/application_view_model.rb', line 67

def translate(key, options = {})
  I18n.translate(key, options)
end