lib/formtastic/inputs/base/collections.rb in formtastic-3.1.3 vs lib/formtastic/inputs/base/collections.rb in formtastic-3.1.4
- old
+ new
@@ -10,11 +10,11 @@
def label_method_from_options
options[:member_label]
end
def value_method
- @value_method ||= (value_method_from_options || label_and_value_method.last)
+ @value_method ||= (value_method_from_options || label_and_value_method[-1])
end
def value_method_from_options
options[:member_value]
end
@@ -22,11 +22,11 @@
def label_and_value_method
@label_and_value_method ||= label_and_value_method_from_collection(raw_collection)
end
def label_and_value_method_from_collection(_collection)
- sample = _collection.first || _collection.last
+ sample = _collection.first || _collection[-1]
case sample
when Array
label, value = :first, :last
when Integer
@@ -41,11 +41,11 @@
[label, value]
end
def raw_collection
- @raw_collection ||= (collection_from_options || collection_from_association || collection_for_boolean)
+ @raw_collection ||= (collection_from_options || collection_from_enum || collection_from_association || collection_for_boolean)
end
def collection
# Return if we have a plain string
return raw_collection if raw_collection.is_a?(String)
@@ -81,16 +81,53 @@
conditions_from_reflection = (reflection.respond_to?(:options) && reflection.options[:conditions]) || {}
conditions_from_reflection = conditions_from_reflection.call if conditions_from_reflection.is_a?(Proc)
scope_conditions = conditions_from_reflection.empty? ? nil : {:conditions => conditions_from_reflection}
where_conditions = (scope_conditions && scope_conditions[:conditions]) || {}
-
+
if Util.rails3?
reflection.klass.scoped(scope_conditions).where({}) # where is uneccessary, but keeps the stubbing simpler while we support rails3
else
reflection.klass.where(where_conditions)
end
end
+ end
+
+ # Assuming the following model:
+ #
+ # class Post < ActiveRecord::Base
+ # enum :status => [ :active, :archived ]
+ # end
+ #
+ # We would end up with a collection like this:
+ #
+ # [["Active", "active"], ["Archived", "archived"]
+ #
+ # The first element in each array uses String#humanize, but I18n
+ # translations are available too. Set them with the following structure.
+ #
+ # en:
+ # activerecord:
+ # attributes:
+ # post:
+ # statuses:
+ # active: Custom Active Label Here
+ # archived: Custom Archived Label Here
+ def collection_from_enum
+ if collection_from_enum?
+ method_name = method.to_s
+
+ enum_options_hash = object.defined_enums[method_name]
+ enum_options_hash.map do |name, value|
+ key = "activerecord.attributes.#{object_name}.#{method_name.pluralize}.#{name}"
+ label = ::I18n.translate(key, :default => name.humanize)
+ [label, name]
+ end
+ end
+ end
+
+ def collection_from_enum?
+ object.respond_to?(:defined_enums) && object.defined_enums.has_key?(method.to_s)
end
def collection_for_boolean
true_text = options[:true] || Formtastic::I18n.t(:yes)
false_text = options[:false] || Formtastic::I18n.t(:no)