Sha256: 7c17dd20ccf80cda89d21d6aec2576534923c05fa8287f11d479e2ed52ebfebb

Contents?: true

Size: 1.39 KB

Versions: 8

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 # LineItem
        @presents = eval(class_name)
        define_method :find_by do
          args[:find_by] || :id
        end
        define_method :"#{name}" 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

8 entries across 8 versions & 1 rubygems

Version Path
faceted-1.6.2 lib/faceted/presenter.rb
faceted-1.6.1 lib/faceted/presenter.rb
faceted-1.6.0 lib/faceted/presenter.rb
faceted-1.5.1 lib/faceted/presenter.rb
faceted-1.5.0 lib/faceted/presenter.rb
faceted-1.4.0 lib/faceted/presenter.rb
faceted-1.3.3 lib/faceted/presenter.rb
faceted-1.3.2 lib/faceted/presenter.rb