Sha256: ddf6a96f05d0990b5842996015842261da8883a39e1669c63ba99d9aed50e9b2

Contents?: true

Size: 1.94 KB

Versions: 6

Compression:

Stored size: 1.94 KB

Contents

# == Schema Information
#
# Table name: filters
#
#  id          :integer          not null, primary key
#  filter_type :string(255)
#  user_id     :integer          not null
#  name        :string(255)      default(""), not null
#  filter      :text             default(""), not null
#  description :text
#  active      :boolean          default(TRUE)
#  position    :integer          default(0), not null
#  created_at  :datetime
#  updated_at  :datetime
#

class Filter < ActiveRecord::Base

  belongs_to :user, inverse_of: :filters

  validates :name,          presence: true,  :uniqueness => { :scope => :filter_type }
  validates :user, :filter, presence: true

  validates :active, inclusion: [true, false]

  default_scope -> { order 'filters.position ASC' }

  scope :active, -> { where(active: true) }

  strip_attributes :collapse_spaces => true

  # exec the filter and resturns the collection

  def results(scope, current_ability, filterql_options = {})
   resource_class = self.filter_type.constantize
   resource_filter, filtered_scope = Basepack::Utils.filter(
     scope,
     { ql: self.filter },
     Basepack::Utils.model_config(resource_class),
     {
       auth_object: current_ability,
       filterql_options: filterql_options
     }
   )
   raise resource_filter.errors[:base] if resource_filter.errors[:base]

   #[resource_filter, filtered_scope]
   filtered_scope
  end

  rails_admin do
    list do
      field :active
      field :name
      field :filter
      field :filter_type
      field :user
    end

    show do
      field :active
      field :name
      field :description
      field :filter
      field :filter_type
      field :position
      field :user
    end

    edit do
      field :active
      field :name
      field :description
      field :filter
      field :filter_type
      field :position
      field :user do
        visible do
          bindings[:view].current_user.has_role? :admin
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
basepack-1.0.1 lib/generators/basepack/templates/filter.rb
basepack-1.0.0 lib/generators/basepack/templates/filter.rb
basepack-1.0.0.pre.0 lib/generators/basepack/templates/filter.rb
basepack-0.2.0 lib/generators/basepack/templates/filter.rb
basepack-0.1.0 lib/generators/basepack/templates/filter.rb
basepack-0.0.2 lib/generators/basepack/templates/filter.rb