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
-
#allowed_param_names(filter_names = [], include_relationships = true) ⇒ Object
Return the complete list of key names that would appear in the form.
-
#create_and_log(user, attributes) ⇒ Object
Record the creation with this user.
-
#create_and_log!(user, attributes) ⇒ Object
Record the creation with this user.
-
#field_names(filter_names) ⇒ Object
allow the model to filter out a name if they want to, meaning the model can return a subset of attribute names.
-
#find_or_create_by_and_log!(user, attributes) ⇒ Object
Find or create by filter, the log the action.
-
#log_change(user, model, changes) ⇒ Object
Log the audit record.
-
#make_options(options) ⇒ Object
Turn the array into a list of options.
-
#many_to_many_associations ⇒ Object
Return a collection of many to many associations.
-
#without_callback(*args, &_block) ⇒ Object
Used by calling ‘model.without_callback(*.args, &block) do’.
Instance Method Details
#allowed_param_names(filter_names = [], include_relationships = true) ⇒ Object
Return the complete list of key names that would appear in the form.
63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/app/models/concerns/standard_model.rb', line 63 def allowed_param_names(filter_names = [], include_relationships = true) # Always filter out the mongoid reserved items filter_names += %w[created_at updated_at _type _id search_text sort_text] associations = many_to_many_associations # filter out the relationship names so we don't have dups associations.each { |association| filter_names << association.keys.first } names = field_names(filter_names) names += associations if include_relationships names.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
138 139 140 141 142 143 144 145 |
# File 'lib/app/models/concerns/standard_model.rb', line 138 def create_and_log(user, attributes) model = new(attributes) model.last_modified_by = user model.created_by = user model.save log_change(user, model, attributes) if model.valid? model end |
#create_and_log!(user, attributes) ⇒ Object
Record the creation with this user
150 151 152 153 154 155 156 157 |
# File 'lib/app/models/concerns/standard_model.rb', line 150 def create_and_log!(user, attributes) model = new(attributes) model.last_modified_by = user model.created_by = user model.save! log_change(user, model, attributes) model end |
#field_names(filter_names) ⇒ Object
allow the model to filter out a name if they want to, meaning the model can return a subset of attribute names
106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/app/models/concerns/standard_model.rb', line 106 def field_names(filter_names) fields.collect do |field| next if filter_names.include?(field[0]) case field[1].[:type].to_s when 'Hash' { field[0] => {} } when 'Array' { field[0] => [] } else field[0] end end.compact end |
#find_or_create_by_and_log!(user, attributes) ⇒ Object
Find or create by filter, the log the action
124 125 126 127 128 129 130 131 132 133 |
# File 'lib/app/models/concerns/standard_model.rb', line 124 def find_or_create_by_and_log!(user, attributes) model = find_or_initialize_by(attributes) if model.new_record? model.last_modified_by = user model.created_by = user model.save! log_change(user, model, attributes) end model end |
#log_change(user, model, changes) ⇒ Object
Log the audit record
162 163 164 165 166 167 |
# File 'lib/app/models/concerns/standard_model.rb', line 162 def log_change(user, model, changes) Web47core::Config.audit_model_log_class.create!(Web47core::Config.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
56 57 58 |
# File 'lib/app/models/concerns/standard_model.rb', line 56 def () .collect { |t| [t.humanize, t] } end |
#many_to_many_associations ⇒ Object
Return a collection of many to many associations. 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.
88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/app/models/concerns/standard_model.rb', line 88 def many_to_many_associations associations = [] reflect_on_all_associations.each do |association| if Mongoid::Compatibility::Version.mongoid7_or_newer? next unless association.relation == Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy else next unless association.macro == :has_and_belongs_to_many end 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 49 50 51 |
# File 'lib/app/models/concerns/standard_model.rb', line 43 def without_callback(*args, &_block) skip_callback(*args) block_result = yield set_callback(*args) block_result rescue StandardError set_callback(*args) nil end |