Class: Flexirails::View
- Inherits:
-
Object
- Object
- Flexirails::View
- Defined in:
- app/models/flexirails/view.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#current_page ⇒ Object
readonly
Returns the value of attribute current_page.
-
#direction ⇒ Object
readonly
Returns the value of attribute direction.
-
#limit ⇒ Object
readonly
Returns the value of attribute limit.
-
#offset ⇒ Object
readonly
Returns the value of attribute offset.
-
#order ⇒ Object
readonly
Returns the value of attribute order.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#per_page ⇒ Object
readonly
Returns the value of attribute per_page.
Instance Method Summary collapse
- #columns ⇒ Object
- #has_next_path ⇒ Object
- #has_prev_path ⇒ Object
- #i18n_default(name) ⇒ Object
- #i18n_scope(clazz = self.class) ⇒ Object
-
#initialize(params) ⇒ View
constructor
A new instance of View.
- #next_pagination_direction(column) ⇒ Object
- #order_query? ⇒ Boolean (also: #order_results?)
- #pagination_hash ⇒ Object
- #query(offset = self.offset, limit = self.limit) ⇒ Object
- #render_column(column, row, context) ⇒ Object
- #rows ⇒ Object
- #sortable_columns ⇒ Object
- #t(name, args = {}) ⇒ Object
- #total ⇒ Object
- #total_page_count ⇒ Object
Constructor Details
#initialize(params) ⇒ View
Returns a new instance of View.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'app/models/flexirails/view.rb', line 5 def initialize params @params = params pagination = params.fetch(:pagination) { params || Hash.new } @current_page = pagination.fetch(:current_page) { 1 }.to_i @per_page = pagination.fetch(:per_page) { 25 }.to_i @order = sanitize(pagination.fetch(:order) { nil }) @direction = sanitize_direction(pagination.fetch(:direction) { nil }) if @current_page > total_page_count @current_page = 1 end @offset = (current_page-1) * per_page @limit = per_page end |
Instance Attribute Details
#current_page ⇒ Object (readonly)
Returns the value of attribute current_page.
3 4 5 |
# File 'app/models/flexirails/view.rb', line 3 def current_page @current_page end |
#direction ⇒ Object (readonly)
Returns the value of attribute direction.
3 4 5 |
# File 'app/models/flexirails/view.rb', line 3 def direction @direction end |
#limit ⇒ Object (readonly)
Returns the value of attribute limit.
3 4 5 |
# File 'app/models/flexirails/view.rb', line 3 def limit @limit end |
#offset ⇒ Object (readonly)
Returns the value of attribute offset.
3 4 5 |
# File 'app/models/flexirails/view.rb', line 3 def offset @offset end |
#order ⇒ Object (readonly)
Returns the value of attribute order.
3 4 5 |
# File 'app/models/flexirails/view.rb', line 3 def order @order end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
3 4 5 |
# File 'app/models/flexirails/view.rb', line 3 def params @params end |
#per_page ⇒ Object (readonly)
Returns the value of attribute per_page.
3 4 5 |
# File 'app/models/flexirails/view.rb', line 3 def per_page @per_page end |
Instance Method Details
#columns ⇒ Object
61 62 63 |
# File 'app/models/flexirails/view.rb', line 61 def columns sortable_columns end |
#has_next_path ⇒ Object
53 54 55 |
# File 'app/models/flexirails/view.rb', line 53 def has_next_path return self.current_page < total_page_count end |
#has_prev_path ⇒ Object
50 51 52 |
# File 'app/models/flexirails/view.rb', line 50 def has_prev_path return self.current_page > 1 end |
#i18n_default(name) ⇒ Object
78 79 80 81 82 83 84 85 86 |
# File 'app/models/flexirails/view.rb', line 78 def i18n_default name scopes = [] clazz = self.class clazz.ancestors.each do |ancestor| break if ancestor == Object scopes << [i18n_scope(ancestor),name].compact.join('.').to_sym end return scopes end |
#i18n_scope(clazz = self.class) ⇒ Object
74 75 76 |
# File 'app/models/flexirails/view.rb', line 74 def i18n_scope clazz = self.class return clazz.name.tableize.singularize.gsub('/','.') end |
#next_pagination_direction(column) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'app/models/flexirails/view.rb', line 22 def next_pagination_direction column if order == column if direction == "DESC" nil else if direction == nil "ASC" else "DESC" end end else "ASC" end end |
#order_query? ⇒ Boolean Also known as: order_results?
65 66 67 |
# File 'app/models/flexirails/view.rb', line 65 def order_query? return order.present? && direction.present? end |
#pagination_hash ⇒ Object
106 107 108 109 110 111 112 113 |
# File 'app/models/flexirails/view.rb', line 106 def pagination_hash return { current_page: self.current_page, per_page: self.per_page, order: self.order, direction: self.direction } end |
#query(offset = self.offset, limit = self.limit) ⇒ Object
46 47 48 |
# File 'app/models/flexirails/view.rb', line 46 def query offset = self.offset, limit = self.limit raise "ImplementationMissing" end |
#render_column(column, row, context) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'app/models/flexirails/view.rb', line 92 def render_column column, row, context method_to_call = "render_#{column.gsub(/\./, '_')}" if self.respond_to?(method_to_call.to_sym) return self.send method_to_call.to_sym, row, context else parts = column.split('.').map(&:to_sym) object = row parts.each do |part| object = object.send(part) end return object end end |
#rows ⇒ Object
70 71 72 |
# File 'app/models/flexirails/view.rb', line 70 def rows return @rows ||= query(offset, limit) end |
#sortable_columns ⇒ Object
57 58 59 |
# File 'app/models/flexirails/view.rb', line 57 def sortable_columns return %w() end |
#t(name, args = {}) ⇒ Object
88 89 90 |
# File 'app/models/flexirails/view.rb', line 88 def t name, args = {} I18n.t([i18n_scope,name].compact.join('.'), { default: i18n_default(name) }.merge(args)) end |
#total ⇒ Object
42 43 44 |
# File 'app/models/flexirails/view.rb', line 42 def total raise "ImplementationMissing" end |
#total_page_count ⇒ Object
38 39 40 |
# File 'app/models/flexirails/view.rb', line 38 def total_page_count return (self.total.to_f / self.per_page.to_f).ceil end |