Class: Mochigome::ReportFocusSettings

Inherits:
Object
  • Object
show all
Defined in:
lib/model_extensions.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ ReportFocusSettings

Returns a new instance of ReportFocusSettings.



185
186
187
188
189
190
191
192
193
# File 'lib/model_extensions.rb', line 185

def initialize(model)
  @model = model
  @options = {}
  @options[:fieldsets] = {}
  @options[:custom_subgroup_exprs] = ActiveSupport::OrderedHash.new
  @options[:custom_assocs] = ActiveSupport::OrderedHash.new
  @options[:ignore_assocs] = Set.new
  @options[:preferred_paths] = {}
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



182
183
184
# File 'lib/model_extensions.rb', line 182

def model
  @model
end

#optionsObject (readonly)

Returns the value of attribute options.



181
182
183
# File 'lib/model_extensions.rb', line 181

def options
  @options
end

#ordering(n) ⇒ Object (readonly)

Returns the value of attribute ordering.



183
184
185
# File 'lib/model_extensions.rb', line 183

def ordering
  @ordering
end

Instance Method Details

#custom_association(tgt_cls, expr) ⇒ Object



243
244
245
# File 'lib/model_extensions.rb', line 243

def custom_association(tgt_cls, expr)
  @options[:custom_assocs][tgt_cls] = expr
end

#custom_subgroup_expression(name, expr) ⇒ Object



239
240
241
# File 'lib/model_extensions.rb', line 239

def custom_subgroup_expression(name, expr)
  @options[:custom_subgroup_exprs][name] = expr
end

#fields(fields) ⇒ Object



214
215
216
# File 'lib/model_extensions.rb', line 214

def fields(fields)
  fieldset(:default, fields)
end

#fieldset(name, fields) ⇒ Object



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/model_extensions.rb', line 218

def fieldset(name, fields)
  unless fields.respond_to?(:each)
    raise ModelSetupError.new "Call f.fieldset with an Enumerable"
  end

  field_descs = fields.map do |f|
    case f
    when String, Symbol then {
      :name => Mochigome::complain_if_reserved_name(f.to_s.strip),
      :value_func => lambda{|obj| obj.send(f.to_sym)}
    }
    when Hash then {
      :name => Mochigome::complain_if_reserved_name(f.keys.first.to_s.strip),
      :value_func => f.values.first.to_proc
    }
    else raise ModelSetupError.new "Invalid field: #{f.inspect}"
    end
  end
  (@options[:fieldsets][name.to_sym] ||= []).concat field_descs
end

#get_orderingObject



210
211
212
# File 'lib/model_extensions.rb', line 210

def get_ordering
  @options[:ordering] || @model.primary_key.to_s
end

#ignore_assoc(name) ⇒ Object

TODO: Unit test for this feature



248
249
250
# File 'lib/model_extensions.rb', line 248

def ignore_assoc(name)
  @options[:ignore_assocs].add name.to_sym
end

#name(n) ⇒ Object



202
203
204
# File 'lib/model_extensions.rb', line 202

def name(n)
  @options[:name] = n.to_proc
end

#preferred_path_to(target_model, assoc_name) ⇒ Object



252
253
254
# File 'lib/model_extensions.rb', line 252

def preferred_path_to(target_model, assoc_name)
  @options[:preferred_paths][target_model.name] = assoc_name
end

#type_name(n) ⇒ Object



195
196
197
198
199
200
# File 'lib/model_extensions.rb', line 195

def type_name(n)
  unless n.is_a?(String)
    raise ModelSetupError.new "Call f.type_name with a String"
  end
  @options[:type_name] = n
end