Sha256: ab19e6f14f3c9f89ec3b26a3834eaf8bb5526fee571279dc346a102ae356b1aa

Contents?: true

Size: 1.52 KB

Versions: 3

Compression:

Stored size: 1.52 KB

Contents

class Tabulatr::Adapter
  def initialize(klaz)
    @base = klaz
    @relation = klaz
  end

  delegate :all, :dup, :count, :limit, :to => :@relation

  def to_sql
    @relation.to_sql if @relation.respond_to? :to_sql
  end

  def class_to_param
    @relation.to_s.downcase.gsub("/","_")
  end

  def preconditions_scope(opts)
    opts[:precondition].present? ? @base.where(opts[:precondition]) : @base
  end

  def order(sortparam, default)
    order_by, order_direction = sort_params(sortparam, default)
    order_by ? { :by => order_by, :direction => order_direction } : nil
  end

  def sort_params(sortparam, default)
    if sortparam
      if sortparam[:_resort]
        order_by = sortparam[:_resort].first.first
        order_direction = sortparam[:_resort].first.last.first.first
      else
        order_by = sortparam.first.first
        order_direction = sortparam.first.last.first.first
      end
      raise "SECURITY violation, sort field name is '#{n}'" unless /^[\w]+$/.match order_direction
      raise "SECURITY violation, sort field name is '#{n}'" unless /^[\d\w]+$/.match order_by
    else
      if default
        l = default.split(" ")
        raise(":default_order parameter should be of the form 'id asc' or 'name desc'.") if l.length == 0 or l.length > 2

        order_by = l[0]
        order_direction = l[1] || 'asc'
      else
        order_by = order_direction = nil
      end
    end

    return order_by, order_direction
  end
end

Dir[File.join(File.dirname(__FILE__), "adapter", "*.rb")].each do |file|
  require file
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
tabulatr2-0.6.0 lib/tabulatr/tabulatr/adapter.rb
tabulatr-0.4.2 lib/tabulatr/tabulatr/adapter.rb
tabulatr-0.4.1 lib/tabulatr/tabulatr/adapter.rb