module CustomFielder::CustomFieldable
Public Class Methods
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
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
Provides a uniform means of accessing data no matter the object, this can be overridden to suit your needs
@note this method can be overridden more specific @note object selection
@return [ActiveRecord::Relation]
# File lib/custom_fielder/custom_fieldable.rb, line 60 def self.custom_fieldable_search all end
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
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
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