Module: SimpleFormObject

Extended by:
ActiveSupport::Concern
Includes:
ActiveModel::Callbacks, ActiveModel::Model
Included in:
Journea::Step
Defined in:
lib/simple_form_object.rb

Overview

Originally from: github.com/reinteractive-open/simple_form_object Copied here (along with specs) so we can add Rails 5 support and tweak as needed

Defined Under Namespace

Modules: ClassMethods Classes: Attribute

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

TODO: Currently having to eat this rubocop error until such time as we understand what form the respond_to_missing? should take rubocop:disable Style/MethodMissing



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/simple_form_object.rb', line 46

def method_missing(method, *args, &block)
  return super unless delegatable?(method)

  # TODO: Figure out why self.class.delegate(method, to: self.class._delegation_target)
  # doesn't work.

  # TODO: At this time don't understand enough about the gem to understand how
  # best to resolve this issue.
  # rubocop:disable Lint/ShadowingOuterLocalVariable
  self.class.send(:define_method, method) do |*args, &block|
    _delegation_target.send(method, *args, &block)
  end
  # rubocop:enable Lint/ShadowingOuterLocalVariable

  send(method, *args, &block)
end

Instance Method Details

#_delegation_targetObject



72
73
74
75
76
77
78
79
80
# File 'lib/simple_form_object.rb', line 72

def _delegation_target
  target = self.class._delegation_target

  if target.is_a? Symbol
    self.send(target)
  else
    target
  end
end

#attribute?(attribute_name) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/simple_form_object.rb', line 86

def attribute?(attribute_name)
  self.class._attribute(attribute_name).present?
end

#attributesObject



97
98
99
100
101
102
103
# File 'lib/simple_form_object.rb', line 97

def attributes
  attribs = {}
  self.class._attributes.each do |a|
    attribs[a.name] = self.send(a.name)
  end
  attribs
end

#column_for_attribute(attribute) ⇒ Object



82
83
84
# File 'lib/simple_form_object.rb', line 82

def column_for_attribute(attribute)
  self.class._attribute(attribute).fake_column
end

#delegatable?(method) ⇒ Boolean

rubocop:enable Style/MethodMissing

Returns:

  • (Boolean)


64
65
66
67
68
69
70
# File 'lib/simple_form_object.rb', line 64

def delegatable?(method)
  if !_delegation_target.nil?
    _delegation_target.respond_to?(method)
  else
    false
  end
end

#initialize(attributes = {}) ⇒ Object



90
91
92
93
94
95
# File 'lib/simple_form_object.rb', line 90

def initialize(attributes = {})
  super
  self.class._attributes.each do |attribute|
    attribute.apply_default_to(self)
  end
end