Sha256: e987ee631fb88e8fddde450a142c9abe8f2a5c72c8649ac21a2c46179cfef828

Contents?: true

Size: 1.13 KB

Versions: 13

Compression:

Stored size: 1.13 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_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.1.9 app/models/carnival/model_helper.rb
carnival-0.1.8 app/models/carnival/model_helper.rb
carnival-0.1.7 app/models/carnival/model_helper.rb
carnival-0.1.6 app/models/carnival/model_helper.rb
carnival-0.1.5 app/models/carnival/model_helper.rb
carnival-0.1.3 app/models/carnival/model_helper.rb
carnival-0.1.2 app/models/carnival/model_helper.rb
carnival-0.1.1 app/models/carnival/model_helper.rb
carnival-0.1.0 app/models/carnival/model_helper.rb
carnival-0.0.59 app/models/carnival/model_helper.rb
carnival-0.0.58 app/models/carnival/model_helper.rb
carnival-0.0.57 app/models/carnival/model_helper.rb
carnival-0.0.56 app/models/carnival/model_helper.rb