Sha256: df79a8b5186168cdcdddcf405b732e79373669404f21e38bea88e3df63864322

Contents?: true

Size: 1.39 KB

Versions: 1

Compression:

Stored size: 1.39 KB

Contents

module Faceted

  module Presenter

    include Faceted::HasObject

    # Class methods ===========================================================

    def self.included(base)
      base.extend ActiveModel::Naming
      base.extend ClassMethods
      base.extend Faceted::Model::ModelClassMethods
      base.send(:attr_accessor, :id)
      base.send(:attr_accessor, :errors)
      base.send(:attr_accessor, :success)
    end

    module ClassMethods

      def klass
        @presents
      end

      def presents(name, args={})
        class_name = args[:class_name] || name.to_s.classify
        @presents = eval(class_name)
        define_method :find_by do
          args[:find_by] || :id
        end
        define_method :"#{class_name.downcase}" do
          object
        end
      end

      def all
        materialize(klass.all)
      end

      def find(id)
        materialize(klass.where(id: id).first)
      end

      def where(args)
        if klass.respond_to? :fields
          if klass.fields.respond_to?(:keys)
            # Mongoid
            attrs = args.select{|k,v| klass.fields.keys.include? k.to_s}
          else
            attrs = args.select{|k,v| klass.fields.include? k.to_s}
          end
        else
          # ActiveRecord et al
          attrs = args.select{|k,v| klass.column_names.include? k.to_s}
        end
        materialize(klass.where(attrs))
      end

    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
faceted-1.3.0 lib/faceted/presenter.rb