module CustomFielder::CustomFieldable

Public Class Methods

custom_fieldable_columns() click to toggle source

Provides a list of columns to be included in an object search. This is to be able to dynamically create tables no matter the object

@note this method should be overridden

@return [Array]

# File lib/custom_fielder/custom_fieldable.rb, line 46
def self.custom_fieldable_columns
  [:id]
end
custom_fieldable_objects(field) click to toggle source

Gets all the avaiable fieldable objects for a model that do not already have a value defined on them

@param field [CustomFielder::Field] field to search for values on

@return [Array<Hash>]

# File lib/custom_fielder/custom_fieldable.rb, line 72
def self.custom_fieldable_objects(field)
  custom_fieldable_search.where.not(id: field.values.select(:fieldable_id))
end
find_objects_with_custom_values_where(value:, field_id:) click to toggle source

Returns all the objects on the model that have the passed value, defined on the field with the passed field_id

@return [ActiveRecord::Relation]

# File lib/custom_fielder/custom_fieldable.rb, line 29
def self.find_objects_with_custom_values_where(value:, field_id:)
  joins(:custom_fielder_values).
  where(custom_fielder_values: {
    value: value,
    field_id: field_id
  })
end

Public Instance Methods

get_custom_fields_with_values() click to toggle source

Returns a hash where the keys are the field names and the values are the field values

@return [Hash]

# File lib/custom_fielder/custom_fieldable.rb, line 82
def get_custom_fields_with_values
  custom_field_values.inject(Hash.new) do |h, field_value|
    field_name = field_value.field.name
    h[field_name] = field_value.value; h
  end
end
value_for_custom_field(field) click to toggle source

Provides a shorter method call to retrieve a custom fields value given the field. Returns the value or nil if the value does not exist

@return [String|Nil]

# File lib/custom_fielder/custom_fieldable.rb, line 96
def value_for_custom_field(field)
  value = self.custom_fielder_values.find_by(field_id: field.id)
  value.nil? ? nil : value.value
end