Class: Flexirails::View

Inherits:
Object
  • Object
show all
Defined in:
app/models/flexirails/view.rb

Direct Known Subclasses

ArrayView

Instance Attribute Summary collapse

Instance Method Summary collapse

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_pageObject (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

#directionObject (readonly)

Returns the value of attribute direction.



3
4
5
# File 'app/models/flexirails/view.rb', line 3

def direction
  @direction
end

#limitObject (readonly)

Returns the value of attribute limit.



3
4
5
# File 'app/models/flexirails/view.rb', line 3

def limit
  @limit
end

#offsetObject (readonly)

Returns the value of attribute offset.



3
4
5
# File 'app/models/flexirails/view.rb', line 3

def offset
  @offset
end

#orderObject (readonly)

Returns the value of attribute order.



3
4
5
# File 'app/models/flexirails/view.rb', line 3

def order
  @order
end

#paramsObject (readonly)

Returns the value of attribute params.



3
4
5
# File 'app/models/flexirails/view.rb', line 3

def params
  @params
end

#per_pageObject (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

#columnsObject



61
62
63
# File 'app/models/flexirails/view.rb', line 61

def columns
  sortable_columns
end

#has_next_pathObject



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_pathObject



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?

Returns:

  • (Boolean)


65
66
67
# File 'app/models/flexirails/view.rb', line 65

def order_query?
  return order.present? && direction.present?
end

#pagination_hashObject



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

#rowsObject



70
71
72
# File 'app/models/flexirails/view.rb', line 70

def rows
  return @rows ||= query(offset, limit)
end

#sortable_columnsObject



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

#totalObject



42
43
44
# File 'app/models/flexirails/view.rb', line 42

def total
  raise "ImplementationMissing"
end

#total_page_countObject



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