Permalink
Browse files

initial commit

  • Loading branch information...
0 parents commit e6ce0630e9d58bc98f4f32c946b8dd5c11b9b52a @triskweline triskweline committed Jan 12, 2012
Showing with 66 additions and 0 deletions.
  1. +8 −0 .gitignore
  2. 0 lib/assignable_values.rb
  3. +58 −0 lib/assignable_values/active_record.rb
@@ -0,0 +1,8 @@
+doc
+pkg
+*.gem
+.idea
+spec/app_root/log/*
+Gemfile.lock
+
+
No changes.
@@ -0,0 +1,58 @@
+module AssignableValues
+ module ActiveRecord
+
+ def self.included(base)
+ base.send :include, InstanceMethods
+ base.send :extend, ClassMethods
+ end
+
+ module ClassMethods
+
+ private
+
+ def assignable_values_for(property, &values)
+ if belongs_to_association?(property)
+ restrict_belongs_to_association(property, values)
+ else
+ restrict_scalar_attribute(property, values)
+ end
+ end
+
+ def belongs_to_association?(association)
+ reflection = reflect_on_association(association)
+ reflection && reflection.macro == :belongs_to
+ end
+
+ def restrict_scalar_attribute(attribute, values)
+ assignable_values_method = "assignable_#{attribute.pluralize}"
+ validate_method = "validate_#{attribute}_assignable"
+ singleton_class.send :define_method, assignable_values_method do
+ instance_eval(&values).collect do |value|
+ value = value.dup
+ def value.human
+ humanize_scalar_attribute_value(attribute, value)
+ end
+ value
+ end
+ end
+ define_method validate_method do
+
+ end
+ end
+
+ def restrict_belongs_to_association(name, values)
+
+ end
+
+ def humanize_scalar_attribute_value(attribute, value)
+ I18n.t("assignable_values.#{name.underscore}.#{attribute}")
+ end
+
+ end
+
+ module InstanceMethods
+
+ end
+
+ end
+endd

0 comments on commit e6ce063

Please sign in to comment.