Class: AdminAssistant::Request::ParamsForSave

Inherits:
Hash
  • Object
show all
Defined in:
lib/admin_assistant/request/base.rb

Defined Under Namespace

Classes: Error, Errors

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(controller, record, record_params = nil) ⇒ ParamsForSave

Returns a new instance of ParamsForSave.


98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/admin_assistant/request/base.rb', line 98

def initialize(controller, record, record_params=nil)
  super()
  @controller, @record_params = controller, record_params
  @model_class_symbol = record.class.name.underscore.to_sym
  @record_params ||= @controller.params[@model_class_symbol]
  @model_methods = record.methods.map(&:to_sym)
  @model_columns = record.class.columns
  @errors = Errors.new
  build_from_split_params
  destroy_params.each do |k,v|
    apply_destroy_param k, v
  end
  build_from_whole_params
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors


96
97
98
# File 'lib/admin_assistant/request/base.rb', line 96

def errors
  @errors
end

Instance Method Details

#apply_destroy_param(name, value) ⇒ Object


113
114
115
116
117
118
119
120
121
# File 'lib/admin_assistant/request/base.rb', line 113

def apply_destroy_param(name, value)
  if whole_params[name].blank?
    self[name] = nil
    mname = "destroy_#{name}_in_attributes"
    if @controller.respond_to?(mname)
      @controller.send(mname, self)
    end
  end
end

#build_from_split_paramsObject


123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/admin_assistant/request/base.rb', line 123

def build_from_split_params
  bases = split_params.map { |k, v| k.gsub(split_param_regex, '') }.uniq
  bases.each do |b|
    h = {}
    from_form_method = "#{b}_from_form".to_sym
    if @controller.respond_to?(from_form_method)
      split_params.each { |k, v|
        if k =~ /#{b}#{split_param_regex.source}/
          h[$1] = split_params.delete(k)
        end
      }
      self[b] = @controller.send(from_form_method, h)
    elsif model_setter?(b)
      split_params.each { |k, v|
        if k =~ /#{b}#{split_param_regex.source}/
          h[k] = split_params.delete(k)
        end
      }
      self.merge! h
    end
  end
end

#build_from_whole_paramsObject


146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/admin_assistant/request/base.rb', line 146

def build_from_whole_params
  whole_params.each do |k, v|
    from_form_method = "#{k}_from_form".to_sym
    if @controller.respond_to?(from_form_method)
      args = [v]
      if @controller.method(from_form_method).arity >= 2
        args << @record_params
      end
      args << @errors if @controller.method(from_form_method).arity >= 3
      self[k] = @controller.send(from_form_method, *args)
    elsif model_setter?(k)
      unless destroy_params[k] && v.blank?
        self[k] = value_from_param(k, v)
      end
    end
  end
end

#destroy_paramsObject


164
165
166
167
168
169
170
171
172
# File 'lib/admin_assistant/request/base.rb', line 164

def destroy_params
  dp = {}
  @record_params.each do |k,v|
    if k =~ %r|(.*)\(destroy\)|
      dp[$1] = v
    end
  end
  dp
end

#model_setter?(attr) ⇒ Boolean

Returns:

  • (Boolean)

174
175
176
177
# File 'lib/admin_assistant/request/base.rb', line 174

def model_setter?(attr)
  @model_columns.any? { |mc| mc.name.to_s == attr } or
      @model_methods.include?("#{attr}=".to_sym)
end

#split_param_key?(key) ⇒ Boolean

Returns:

  • (Boolean)

179
180
181
# File 'lib/admin_assistant/request/base.rb', line 179

def split_param_key?(key)
  key =~ split_param_regex
end

#split_param_regexObject


183
184
185
# File 'lib/admin_assistant/request/base.rb', line 183

def split_param_regex
  @split_param_regex ||= /\((.+i)\)$/
end

#split_paramsObject


187
188
189
190
191
# File 'lib/admin_assistant/request/base.rb', line 187

def split_params
  sp = {}
  @record_params.each do |k,v| sp[k] = v if split_param_key?(k); end
  sp
end

#value_from_param(name, str) ⇒ Object


193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/admin_assistant/request/base.rb', line 193

def value_from_param(name, str)
  column = @model_columns.detect { |c| c.name == name }
  if column && column.type == :boolean
    if str == '1'
      true
    elsif str == '0'
      false
    else
      nil
    end
  else
    str
  end
end

#whole_paramsObject


208
209
210
211
212
213
214
215
216
# File 'lib/admin_assistant/request/base.rb', line 208

def whole_params
  wp = {}
  @record_params.each do |k,v|
    unless split_param_key?(k) || k =~ %r|(.*)\(destroy\)|
      wp[k] = v
    end
  end
  wp
end