Class: Tableling::View

Inherits:
Object
  • Object
show all
Defined in:
lib/tableling-rails/view.rb

Defined Under Namespace

Classes: DSL

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, config, options = {}, &block) ⇒ View

Returns a new instance of View.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/tableling-rails/view.rb', line 8

def initialize name, config, options = {}, &block

  @name, @config = name, config
  @fields = []

  @base_query = @config.model
  @base_count_query = nil

  @settings = Settings.new @config.settings

  if block
    dsl = DSL.new self
    dsl.extend @settings.dsl
    dsl.instance_eval &block
  end
end

Instance Attribute Details

#base_count_queryObject

Returns the value of attribute base_count_query.



6
7
8
# File 'lib/tableling-rails/view.rb', line 6

def base_count_query
  @base_count_query
end

#base_queryObject

Returns the value of attribute base_query.



6
7
8
# File 'lib/tableling-rails/view.rb', line 6

def base_query
  @base_query
end

#configObject (readonly)

Returns the value of attribute config.



5
6
7
# File 'lib/tableling-rails/view.rb', line 5

def config
  @config
end

#fieldsObject (readonly)

Returns the value of attribute fields.



5
6
7
# File 'lib/tableling-rails/view.rb', line 5

def fields
  @fields
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/tableling-rails/view.rb', line 5

def name
  @name
end

#quick_search_blockObject

Returns the value of attribute quick_search_block.



6
7
8
# File 'lib/tableling-rails/view.rb', line 6

def quick_search_block
  @quick_search_block
end

#serialize_record_blockObject

Returns the value of attribute serialize_record_block.



6
7
8
# File 'lib/tableling-rails/view.rb', line 6

def serialize_record_block
  @serialize_record_block
end

#serialize_response_blockObject

Returns the value of attribute serialize_response_block.



6
7
8
# File 'lib/tableling-rails/view.rb', line 6

def serialize_response_block
  @serialize_response_block
end

#settingsObject (readonly)

Returns the value of attribute settings.



5
6
7
# File 'lib/tableling-rails/view.rb', line 5

def settings
  @settings
end

Instance Method Details

#field(name) ⇒ Object



25
26
27
# File 'lib/tableling-rails/view.rb', line 25

def field name
  @fields.find{ |f| f.name.to_s == name.to_s }
end

#process(options = {}) ⇒ Object

Raises:



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/tableling-rails/view.rb', line 29

def process options = {}
  
  q = options[:base] || @base_query
  cq = options[:base_count] || @base_count_query

  if @quick_search_block and options[:quickSearch].present?
    q = @quick_search_block.call q, options[:quickSearch].to_s
    cq = @quick_search_block.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 ' '
      # TODO: handle unknown fields
      f = @fields.find{ |f| f.working_name.to_s == parts[0] }
      q = f.with_order q, parts[1].downcase.to_sym if f
    end
  end

  @fields.each{ |f| q = f.with_includes q }

  response_options = (options[:response] || {}).merge({
    total: total
  })

  page_size = options[:pageSize].to_i
  page_size = 10 if page_size <= 0
  q = q.limit page_size

  max_page = (total.to_f / page_size).ceil
  page = options[:page].to_i

  # TODO: allow this to be disabled
  if page < 1 or page > max_page
    page = 1
    response_options[:page] = 1
  end

  q = q.offset (page - 1) * page_size

  serialize_response q, response_options
end