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.



83
84
85
# File 'lib/app/models/concerns/standard_model.rb', line 83

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.



60
61
62
63
64
65
66
67
68
69
# File 'lib/app/models/concerns/standard_model.rb', line 60

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

#create_and_log(user, attributes) ⇒ Object

Record the creation with this user



122
123
124
125
126
127
128
# File 'lib/app/models/concerns/standard_model.rb', line 122

def create_and_log(user, attributes)
  attributes[:last_modified_by_id] = user.id
  attributes[:created_by_id] = user.id
  model = create(attributes)
  log_change(user, model, attributes) if model.valid?
  model
end

#create_and_log!(user, attributes) ⇒ Object

Record the creation with this user



133
134
135
136
137
138
139
# File 'lib/app/models/concerns/standard_model.rb', line 133

def create_and_log!(user, attributes)
  attributes[:last_modified_by_id] = user.id
  attributes[:created_by_id] = user.id
  model = create!(attributes)
  log_change(user, model, attributes)
  model
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



75
76
77
# File 'lib/app/models/concerns/standard_model.rb', line 75

def field_names
  attribute_names
end

#find_or_create_by_and_log!(user, attributes) ⇒ Object

Find or create by filter, the log the action



112
113
114
115
116
117
# File 'lib/app/models/concerns/standard_model.rb', line 112

def find_or_create_by_and_log!(user, attributes)
  model = find_or_initialize_by(attributes)
  log_change(user, model, attributes) if model.new_record? && model.valid?
  model.save!
  model
end

#log_change(user, model, changes) ⇒ Object

Log the audit record



144
145
146
147
148
149
# File 'lib/app/models/concerns/standard_model.rb', line 144

def log_change(user, model, changes)
  Web47core::Config.user_audit_model_log_class.create!(Web47core::Config.user_audit_model => user,
                                                       model: model,
                                                       action: model.audit_action,
                                                       changed_values: App47Logger.clean_params(changes).to_json)
end

#make_options(options) ⇒ Object

Turn the array into a list of options



53
54
55
# File 'lib/app/models/concerns/standard_model.rb', line 53

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.



99
100
101
102
103
104
105
106
107
# File 'lib/app/models/concerns/standard_model.rb', line 99

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’



43
44
45
46
47
48
# File 'lib/app/models/concerns/standard_model.rb', line 43

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