Sha256: 0170cc096c37fddc1b6a8c137b58e3b60c08f577d96c37390396e11bdef29949

Contents?: true

Size: 882 Bytes

Versions: 4

Compression:

Stored size: 882 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

4 entries across 4 versions & 1 rubygems

Version Path
findable-0.2.1 lib/findable/associations/utils.rb
findable-0.2.0 lib/findable/associations/utils.rb
findable-0.1.5 lib/findable/associations/utils.rb
findable-0.1.4 lib/findable/associations/utils.rb