Class: Tableling::View
- Inherits:
-
Object
- Object
- Tableling::View
- Defined in:
- lib/tableling-rails/view.rb
Defined Under Namespace
Classes: DSL
Instance Attribute Summary collapse
-
#base_count_query ⇒ Object
Returns the value of attribute base_count_query.
-
#base_query ⇒ Object
Returns the value of attribute base_query.
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#quick_search_block ⇒ Object
Returns the value of attribute quick_search_block.
-
#serialize_record_block ⇒ Object
Returns the value of attribute serialize_record_block.
-
#serialize_response_block ⇒ Object
Returns the value of attribute serialize_response_block.
-
#settings ⇒ Object
readonly
Returns the value of attribute settings.
Instance Method Summary collapse
- #field(name) ⇒ Object
-
#initialize(name, config, options = {}, &block) ⇒ View
constructor
A new instance of View.
- #process(options = {}) ⇒ Object
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, = {}, &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_query ⇒ Object
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_query ⇒ Object
Returns the value of attribute base_query.
6 7 8 |
# File 'lib/tableling-rails/view.rb', line 6 def base_query @base_query end |
#config ⇒ Object (readonly)
Returns the value of attribute config.
5 6 7 |
# File 'lib/tableling-rails/view.rb', line 5 def config @config end |
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
5 6 7 |
# File 'lib/tableling-rails/view.rb', line 5 def fields @fields end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
5 6 7 |
# File 'lib/tableling-rails/view.rb', line 5 def name @name end |
#quick_search_block ⇒ Object
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_block ⇒ Object
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_block ⇒ Object
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 |
#settings ⇒ Object (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
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 = {} q = [:base] || @base_query cq = [:base_count] || @base_count_query if @quick_search_block and [:quickSearch].present? q = @quick_search_block.call q, [:quickSearch].to_s cq = @quick_search_block.call cq, [:quickSearch].to_s if cq end total = (cq || q).count raise BaseQueryError, "Count query must return a number" unless total.kind_of?(Fixnum) if [:sort].present? [: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] || {}).merge({ total: total }) page_size = [:pageSize].to_i page_size = 10 if page_size <= 0 q = q.limit page_size max_page = (total.to_f / page_size).ceil page = [:page].to_i # TODO: allow this to be disabled if page < 1 or page > max_page page = 1 [:page] = 1 end q = q.offset (page - 1) * page_size serialize_response q, end |