Module: QUI::CommonHelpers::Model::ClassMethods

Defined in:
lib/qui-common-helpers.rb

Instance Method Summary collapse

Instance Method Details

#enum_human_value(attribute_name, value) ⇒ Object

Raises:

  • (ArgumentError)


62
63
64
65
66
# File 'lib/qui-common-helpers.rb', line 62

def enum_human_value(attribute_name, value)
  raise ArgumentError, "Attribute #{attribute_name} is not defined as enum with validates_enum_of" unless is_enum? attribute_name
  
  I18n.t(:"activerecord.enums.#{self.to_s.tableize.singularize}.#{attribute_name}.#{value}")
end


84
85
86
87
88
89
# File 'lib/qui-common-helpers.rb', line 84

def has_link_default_description(field)
  @link_default_description_field = field
  class << self
    attr_reader :link_default_description_field
  end
end

#is_enum?(attribute_name) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
60
# File 'lib/qui-common-helpers.rb', line 57

def is_enum?(attribute_name)
  return false unless @enums
  @enums.include? attribute_name.to_sym
end

#validates_enum_of(attribute_name, options = {}) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/qui-common-helpers.rb', line 68

def validates_enum_of(attribute_name, options = {})
  options.symbolize_keys!
  
  
  @enums ||= []
  @enums << attribute_name.to_sym

  validation_options = { :in => I18n.t("activerecord.enums.#{table_name.singularize}.#{attribute_name}").collect{|k,v| k.to_s.to_i } }
  [ :allow_nil, :allow_blank, :if, :message, :unless ].each do |option|
    validation_options[option] = options[option] if options.has_key? option
  end
  
  validates_inclusion_of attribute_name, validation_options
end