Module: Records::Base

Extended by:
ActiveSupport::Concern
Included in:
ApplicationRecord
Defined in:
app/models/concerns/records/base.rb

Instance Method Summary collapse

Instance Method Details

#all_blank?(attributes = {}) ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
72
73
74
75
76
# File 'app/models/concerns/records/base.rb', line 69

def all_blank?(attributes = {})
  attributes = self.attributes if attributes.empty?
  attributes.all? do |key, value|
    key == "_destroy" || value.blank? ||
      value.is_a?(Hash) && all_blank?(value) ||
      value.is_a?(Array) && value.all? { |val| all_blank?(val) }
  end
end

#label_stringObject

this is a template method you can override in activerecord models if we shouldn’t just use their first string to identify them.



49
50
51
52
53
54
55
# File 'app/models/concerns/records/base.rb', line 49

def label_string
  if (label_attribute = self.class.label_attribute)
    send(:"#{label_attribute}_was")
  else
    self.class.name.underscore.split("/").last.titleize
  end
end

#parent_collectionObject



57
58
59
60
61
62
63
# File 'app/models/concerns/records/base.rb', line 57

def parent_collection
  # TODO Try to suggest what the entire method definition should actually be
  # using parent_key below to do so.
  model_name = self.class
  # parent_key = model_name.reflect_on_all_associations(:belongs_to).first.name
  raise "You're trying to use a feature that requires #{model_name} to have a `collection` method defined that returns the Active Record association that this model belongs to within its parent object."
end

#seeding?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'app/models/concerns/records/base.rb', line 65

def seeding?
  Rake::Task.task_defined?("db:seed") && Rake::Task["db:seed"].already_invoked
end