Sha256: 1793143163b1e2909c3ce910708ae5e02bee820857cbca8a96e5896eee996bc6

Contents?: true

Size: 1.68 KB

Versions: 1

Compression:

Stored size: 1.68 KB

Contents

module Selections
  module BelongsToSelection

    # Helper for belongs_to and accepts all the standard rails options
    #
    # Example
    #   class Thing < ActiveRecord::Base
    #     belongs_to_selection :priority
    #
    # by default adds - class_name: "Selection"
    #
    # This macro also adds a number of methods onto the class if there is a selection
    # named as the class underscore name (eg: "thing_priority"), then methods are created
    # for all of the selection values under that parent. For example:
    #
    #   thing = Thing.find(x)
    #   thing.priority = Selection.thing_priority_high
    #   thing.priority_high? #=> true
    #   thing.priority_low?  #=> false
    #
    # thing.priority_high? is equivalent to thing.priority == Selection.thing_priority_high
    # except that the id of the selection is cached at the time the class is loaded.
    #
    # Note that this is only appropriate to use for system selection values that are known
    # at development time, and not to values that the users can edit in the live system.
    def belongs_to_selection(target, options={})
      belongs_to target, options.merge(:class_name => "Selection")

      prefix = self.name.downcase
      parent = Selection.where(system_code: "#{prefix}_#{target}").first
      if parent
        target_id = "#{target}_id".to_sym
        parent.children.each do |s|
          method_name = "#{s.system_code.sub("#{prefix}_", '')}?".to_sym
          class_eval do
            define_method method_name do
              send(target_id) == s.id
            end
          end
        end
      end
    end

    ActiveSupport.on_load :active_record do
      extend Selections::BelongsToSelection
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
selections-0.1.14 lib/selections/belongs_to_selection.rb