Sha256: c81f97a989dbb8c53a51eb016031be2647a7487818363894f832efe52d6e172d

Contents?: true

Size: 1.26 KB

Versions: 13

Compression:

Stored size: 1.26 KB

Contents

module Carnival::ModelHelper

  def to_label
    self.respond_to?(:name) ? self.name : self.to_s
  end

  def self.included mod
    mod.extend ClassMethods
  end

  module ClassMethods
    def list_for_select(params = {})
      select = []
      select << ['', ''] if params[:add_empty_option]
      query = all
      query = query.where(params[:query]) if params[:query].present?
      if params[:reverse]
        select.concat query.collect{|c|[c.to_label, c.id]}
      else
        select.concat query.collect{|c|[c.id, c.to_label]}
      end
      select
    end

    def list_for_search
      select = []
      select << ['', '']
      select.concat all.collect{|c|[c.to_label, c.to_label]}
    end

    def list_for_checkbox
      list = []
      all.each {|object| list << [object.id, object.to_label] }
      list
    end

    def get_elements_for_select(params = {})
      return all if params[:carnival_scope].nil?
      scopes = params[:carnival_scope][:scopes]
      return all if scopes.empty?
      model_object = params[:carnival_scope][:model_object]
      conditions = {}
      scopes.each do |scp|
        value = model_object.send scp
        conditions[scp] = value if !value.nil?
      end
      return where(conditions) if !conditions.empty?
      []
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
carnival-0.3.2 app/models/carnival/model_helper.rb
carnival-0.3.1 app/models/carnival/model_helper.rb
carnival-0.3.0 app/models/carnival/model_helper.rb
carnival-0.2.8 app/models/carnival/model_helper.rb
carnival-0.2.7 app/models/carnival/model_helper.rb
carnival-0.2.6 app/models/carnival/model_helper.rb
carnival-0.2.5 app/models/carnival/model_helper.rb
carnival-0.2.4 app/models/carnival/model_helper.rb
carnival-0.2.3 app/models/carnival/model_helper.rb
carnival-0.1.10 app/models/carnival/model_helper.rb
carnival-0.2.2 app/models/carnival/model_helper.rb
carnival-0.2.1 app/models/carnival/model_helper.rb
carnival-0.2.0 app/models/carnival/model_helper.rb