Sha256: bddeebc82610ab5f61882d4b7a808b29f3f6e9661e03681e81068eaba1954d28

Contents?: true

Size: 1.58 KB

Versions: 1

Compression:

Stored size: 1.58 KB

Contents

require 'json'

module ActsAsAwardable

  def self.included base
    base.extend ClassMethods
  end

  module ClassMethods

    # acts_as_awardable :delegate_to => :col, :prefix => true/false
    def acts_as_awardable options={}

      raise "ActsAsAwardable don't konw wihci column to delegate to" if options[:delegated_to].blank? 
      if options[:delegate_to].is_a?(Array) && (options[:prefix].blank? || options[:prefix] == false)
        raise "Multiple delegated columns detected, please set :prefix => [:prefix_a, :prefix_b, ..] explictly"  
      end

      delegated_columns = ([] << options[:delegated_to]).flatten
      prefixes = ([] << options[:prefix]).flatten

      delegated_columns.each_with_index do |col, idx|
  
        prefix = (prefixes[idx].to_s + "_") if prefixes[idx]
  
        module_eval <<-EOF	
  
          def #{prefix}award_attributes       
            @#{prefix}award_attributes ||= JSON.parse(self.send("#{col}".to_sym) || '{}')
          end
  
          def #{prefix}award_attributes= values_hash
            result = if values_hash.blank?
              {}
            else 
              transaction do
                #{prefix}award_attributes.delete_if{|k,v| !values_hash.keys.include?(k.to_s) }
                values_hash.each_pair do |award_type,values|
                  #{prefix}award_attributes[award_type.to_s] = values
                end
              end
            end
          end
  
          def after_validation
            super
            self.send("#{col}=", @#{prefix}award_attributes.to_json)
          end
  
        EOF
  
      end

    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
acts_as_awardable-0.1.0 lib/asa/acts_as_awardable.rb