Module: TransForms::MainModel::Proxy::ClassMethods

Defined in:
lib/trans_forms/main_model/proxy.rb

Constant Summary collapse

RESERVED_COLUMN_NAMES =
%w(id created_at updated_at)
REJECT_COLUMN_PROC =
Proc.new { |c| RESERVED_COLUMN_NAMES.include?(c.name) }

Instance Method Summary collapse

Instance Method Details

#column_type(type) ⇒ Object

Returns a Class for the specific column type.



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/trans_forms/main_model/proxy.rb', line 90

def column_type(type)
  case type
    when :integer                     then Integer
    when :float, :decimal             then Float
    when :string, :text, :uuid        then String
    when :datetime, :timestamp, :time then DateTime
    when :date                        then Date
    when :boolean                     then Virtus::Attribute::Boolean # Boolean is not a standard Ruby class
    else
      raise "Could not match column type '#{type}' for #{model_name}"
  end
end

#configure_proxy(proxy_options) ⇒ Object

Configures the proxy setup. Called from set_main_model when the option :proxy was supplied. proxy_options will be the value for that :proxy key.

If +proxy



54
55
56
57
58
# File 'lib/trans_forms/main_model/proxy.rb', line 54

def configure_proxy(proxy_options)
  if proxy_options.is_a?(Hash)
    set_proxy_attributes proxy_options[:attributes] if proxy_options[:attributes]
  end
end

#model_nameObject

Returns an ActiveModel::Name object for module. It can be used to retrieve all kinds of naming-related information (See ActiveModel::Name for more information).

class PostForm < TransForms::FormBase
  set_main_model :post, proxy: true
end

PostForm.model_name          # => Post
PostForm.model_name.class    # => ActiveModel::Name
PostForm.model_name.singular # => "post"
PostForm.model_name.plural   # => "posts"


115
116
117
118
119
120
121
122
123
124
# File 'lib/trans_forms/main_model/proxy.rb', line 115

def model_name
  @_model_name ||= begin
    klass = respond_to?(:main_class) ? main_class : self

    namespace = klass.parents.detect do |n|
      n.respond_to?(:use_relative_model_naming?) && n.use_relative_model_naming?
    end
    ActiveModel::Name.new(klass, namespace)
  end
end

#proxy_columns(columns) ⇒ Object

Given a set of ActiveRecord Columns, will setup attributes according to the Virtus standard that will have the default value of the main instance record.



80
81
82
83
84
85
86
87
# File 'lib/trans_forms/main_model/proxy.rb', line 80

def proxy_columns(columns)
  columns.each do |column|
    if (type = column_type(column.type)).present?
      # When setting the default value, note that +main_instance+ might be nil, so we have to use +try+
      attribute column.name,  type, default: proc { |f| f.main_instance.try(column.name) }
    end
  end
end

#set_proxy_attributes(attributes) ⇒ Object

If attributes is the Boolean value TRUE then we will proxy all of the attributes from the main_model class except for a few reserved attributes.

But if attributes is an array then we only proxy those attributes listed in the array. If any of the attributes listed in the array is not an actual attribute of the main model then an Error will be raised.



68
69
70
71
72
73
74
75
# File 'lib/trans_forms/main_model/proxy.rb', line 68

def set_proxy_attributes(attributes)
  if attributes == :all
    proxy_columns main_class.columns.reject(&REJECT_COLUMN_PROC)
  elsif attributes.is_a?(Array)
    attr_names = attributes.map(&:to_s)
    proxy_columns main_class.columns.reject(&REJECT_COLUMN_PROC).select { |c| attr_names.include?(c.name) }
  end
end