Sha256: fdcd1066ce0719265218baf9344b5a34854fae7aa5d2f32d82bb5943ff008020

Contents?: true

Size: 883 Bytes

Versions: 2

Compression:

Stored size: 883 Bytes

Contents

module Findable
  module Associations
    class Utils
      def self.model_for(name, options = {})
        unless model_name = options[:class_name].presence
          name = options[:collection] ? name.to_s.singularize : name.to_s
          model_name = name.camelize
        end

        if options[:safe]
          model_name.try(:safe_constantize)
        else
          model_name.constantize
        end
      end

      def self.parse_args(args)
        copied = args.dup
        options = copied.extract_options!
        [copied.first, options]
      end

      def self.add_reflection(macro, name, options, ar)
        reflection = ActiveRecord::Reflection.create(
          macro.to_sym,
          name.to_sym,
          nil,
          options,
          ar
        )
        ActiveRecord::Reflection.add_reflection(ar, name.to_sym, reflection)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
findable-0.1.3 lib/findable/associations/utils.rb
findable-0.1.2 lib/findable/associations/utils.rb