module QbwcRequests::OrderedFields::ClassMethods

Public Instance Methods

attr_order() click to toggle source
# File lib/qbwc_requests/ordered_fields.rb, line 38
def attr_order
  @attr_order
end
field(attribute_name) click to toggle source
# File lib/qbwc_requests/ordered_fields.rb, line 4
def field attribute_name
  @attr_order ||= Set.new
  @attr_order << attribute_name
  attr_accessor attribute_name
end
has_one(attribute_name, klass) click to toggle source
# File lib/qbwc_requests/ordered_fields.rb, line 9
def has_one attribute_name, klass
  @attr_order ||= Set.new
  @attr_order << attribute_name
  attr_accessor attribute_name
  validates_each attribute_name do |record, attribute, value|
    if !value.blank? and value.class != klass
      record.errors.add(attribute, "must be of type #{klass}")
    end
  end
end
ref_to(attribute_prefix, name_length=nil) click to toggle source
# File lib/qbwc_requests/ordered_fields.rb, line 19
def ref_to attribute_prefix, name_length=nil
  attribute_name = "#{attribute_prefix}_ref".to_sym
  @attr_order ||= Set.new
  @attr_order << attribute_name
  attr_accessor attribute_name
  validates_each attribute_name do |record, attribute, value|
    if value.present?
      if value.is_a?(Hash)
        if !(value[:list_id].present? ^ value[:full_name].present?)
          record.errors.add(attribute, "Must have list_id or full_name")
        elsif name_length && value.fetch(:full_name, "").length > name_length
          record.errors.add(attribute, "- maximum 'full name' length is: #{name_length}")
        end
      else
        record.errors.add(attribute, "Must have the format {list_id: 'value'} or {full_name: 'value'}")
      end
    end
  end
end