Sha256: 4b2ea650dcb3330dc952f379fd4cf6a5aa173f1e3718325222a0d32d35c73f7a
Contents?: true
Size: 1.51 KB
Versions: 1
Compression:
Stored size: 1.51 KB
Contents
# Poly Belongs To # The MIT License (MIT) # Copyright (C) 2015 by Daniel P. Clark $: << File.join(File.dirname(__FILE__), "/poly_belongs_to") require 'poly_belongs_to/version' require 'active_support/concern' module PolyBelongsTo extend ActiveSupport::Concern included do def self.pbt reflect_on_all_associations(:belongs_to).first.try(:name) end def self.poly? !!reflect_on_all_associations(:belongs_to).first.try {|i| i.options[:polymorphic] } end def self.pbt_params_name(allow_as_nested = true) if poly? allow_as_nested ? "#{table_name}_attributes".to_sym : name.downcase.to_sym else name.downcase.to_sym end end def self.pbt_id_sym val = pbt val ? "#{val}_id".to_sym : nil end def self.pbt_type_sym poly? ? "#{pbt}_type".to_sym : nil end end def pbt self.class.pbt end def poly? self.class.poly? end def pbt_id val = pbt val ? eval("self.#{val}_id") : nil end def pbt_type poly? ? eval("self.#{pbt}_type") : nil end def pbt_parent val = pbt if pbt if poly? eval "#{pbt_type}.find(#{pbt_id})" else eval "#{val.capitalize.to_s}.find(#{pbt_id})" end else nil end end def pbt_id_sym self.class.pbt_id_sym end def pbt_type_sym self.class.pbt_type_sym end def pbt_params_name(allow_as_nested = true) self.class.pbt_params_name(allow_as_nested) end end ActiveRecord::Base.send(:include, PolyBelongsTo)
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
poly_belongs_to-0.1.3 | lib/poly_belongs_to.rb |