Module: StandardModel::ClassMethods

Defined in:
lib/app/models/concerns/standard_model.rb

Overview

Mixin to add methods to the classes themselves Used by calling model.method_name

Instance Method Summary collapse

Instance Method Details

#all_associationsObject

gather up the collections we care about and return them. For now, the many to many associations are the ones that need some extra help.



70
71
72
# File 'lib/app/models/concerns/standard_model.rb', line 70

def all_associations
  many_to_many_associations
end

#allowed_param_names(filter_names = []) ⇒ Object

Return the complete list of key names that would appear in the form.



47
48
49
50
51
52
53
54
55
56
# File 'lib/app/models/concerns/standard_model.rb', line 47

def allowed_param_names(filter_names = [])
  # Always filter out the mongoid reserved items
  filter_names += %w[created_at updated_at _type _id]
  associations = all_associations
  # filter out the relationship names so we don't have dups
  associations.each { |association| filter_names << association.keys.first }
  (field_names + associations).delete_if { |name| filter_names.include?(name) }
rescue StandardError
  attribute_names.delete_if { |name| filter_names.include?(name) }
end

#field_namesObject

allow the model to filter out a name if they want to, meaning the model can return a subset of attribute names



62
63
64
# File 'lib/app/models/concerns/standard_model.rb', line 62

def field_names
  attribute_names
end

#make_options(options) ⇒ Object

Turn the array into a list of options



40
41
42
# File 'lib/app/models/concerns/standard_model.rb', line 40

def make_options(options)
  options.collect { |t| [t, t.humanize] }
end

#many_to_many_associationsObject

Return a collection of many to many assocations. We basically need to turn the current value returned by attribute names

relationship_ids

to

{ relationship_ids => [] }

Telling the permit command to accept the value as an array of items.



86
87
88
89
90
91
92
93
94
# File 'lib/app/models/concerns/standard_model.rb', line 86

def many_to_many_associations
  associations = []
  reflect_on_all_associations.each do |association|
    next unless association.macro == :has_and_belongs_to_many

    associations << { association.key => [] }
  end
  associations
end

#without_callback(*args, &_block) ⇒ Object

Used by calling ‘model.without_callback(*.args, &block) do’



30
31
32
33
34
35
# File 'lib/app/models/concerns/standard_model.rb', line 30

def without_callback(*args, &_block)
  skip_callback(*args)
  result = yield
  set_callback(*args)
  result
end