Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

initial commit

  • Loading branch information...
commit e6ce0630e9d58bc98f4f32c946b8dd5c11b9b52a 0 parents
Henning Koch henning-koch authored
8 .gitignore
... ... @@ -0,0 +1,8 @@
  1 +doc
  2 +pkg
  3 +*.gem
  4 +.idea
  5 +spec/app_root/log/*
  6 +Gemfile.lock
  7 +
  8 +
0  lib/assignable_values.rb
No changes.
58 lib/assignable_values/active_record.rb
... ... @@ -0,0 +1,58 @@
  1 +module AssignableValues
  2 + module ActiveRecord
  3 +
  4 + def self.included(base)
  5 + base.send :include, InstanceMethods
  6 + base.send :extend, ClassMethods
  7 + end
  8 +
  9 + module ClassMethods
  10 +
  11 + private
  12 +
  13 + def assignable_values_for(property, &values)
  14 + if belongs_to_association?(property)
  15 + restrict_belongs_to_association(property, values)
  16 + else
  17 + restrict_scalar_attribute(property, values)
  18 + end
  19 + end
  20 +
  21 + def belongs_to_association?(association)
  22 + reflection = reflect_on_association(association)
  23 + reflection && reflection.macro == :belongs_to
  24 + end
  25 +
  26 + def restrict_scalar_attribute(attribute, values)
  27 + assignable_values_method = "assignable_#{attribute.pluralize}"
  28 + validate_method = "validate_#{attribute}_assignable"
  29 + singleton_class.send :define_method, assignable_values_method do
  30 + instance_eval(&values).collect do |value|
  31 + value = value.dup
  32 + def value.human
  33 + humanize_scalar_attribute_value(attribute, value)
  34 + end
  35 + value
  36 + end
  37 + end
  38 + define_method validate_method do
  39 +
  40 + end
  41 + end
  42 +
  43 + def restrict_belongs_to_association(name, values)
  44 +
  45 + end
  46 +
  47 + def humanize_scalar_attribute_value(attribute, value)
  48 + I18n.t("assignable_values.#{name.underscore}.#{attribute}")
  49 + end
  50 +
  51 + end
  52 +
  53 + module InstanceMethods
  54 +
  55 + end
  56 +
  57 + end
  58 +endd

0 comments on commit e6ce063

Please sign in to comment.
Something went wrong with that request. Please try again.