Sha256: e891a2d3b4af95760f381baf499dd2e9af045f9c194df85bdab96882d21c18fd
Contents?: true
Size: 1.99 KB
Versions: 6
Compression:
Stored size: 1.99 KB
Contents
module Tableling class View attr_reader :name, :config, :settings, :base_query, :base_count_query def initialize name, config, options = {}, &block @name, @config = name, config @fields = [] @base_query = @config.model @base_count_query = nil @settings = Settings.new @config.settings extend @settings.dsl instance_eval &block if block @frozen = true end def field name, options = {}, &block return @fields.find{ |f| f.name.to_s == name.to_s } if @frozen @fields.delete_if{ |f| f.name.to_s == name.to_s } Field.new(name, self, options, &block).tap{ |f| @fields << f } end def quick_search &block @quick_search = block end def base base_query @base_query = base_query end def base_count base_count_query @base_count_query = base_count_query end def process options = {} q = options[:base] || @base_query cq = options[:base_count] || @base_count_query if @quick_search and options[:quickSearch].present? q = @quick_search.call q, options[:quickSearch].to_s cq = @quick_search.call cq, options[:quickSearch].to_s if cq end total = (cq || q).count raise BaseQueryError, "Count query must return a number" unless total.kind_of?(Fixnum) if options[:sort].present? options[:sort].select{ |item| item.match /\A([^ ]+)* (asc|desc)\Z/i }.each do |item| parts = item.split ' ' f = field parts[0] q = f.with_order q, parts[1].downcase.to_sym if f end end @fields.each{ |f| q = f.with_includes q } limit = options[:pageSize].to_i limit = 10 if limit <= 0 q = q.limit limit offset = options[:page].to_i - 1 offset = 0 if offset < 0 q = q.offset offset * limit { :total => total, :data => q.all.collect{ |o| @fields.inject({}){ |memo,f| memo[f.name] = f.extract(o); memo } } } end end end
Version data entries
6 entries across 6 versions & 1 rubygems