Module: IndexView::Implementation

Includes:
SQLConditions
Included in:
Base
Defined in:
lib/index_view/implementation.rb

Constant Summary collapse

ASC =
:ASC
DESC =
:DESC
SORT_DIRECTIONS =
[ASC, DESC]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SQLGenerator

#like_for_many_columns

Instance Attribute Details

#paramsObject (readonly)

Returns the value of attribute params.



9
10
11
# File 'lib/index_view/implementation.rb', line 9

def params
  @params
end

Instance Method Details

#all(*args) ⇒ Object



33
34
35
# File 'lib/index_view/implementation.rb', line 33

def all(*args)
  find(:all, *args)
end

#ascending?Boolean

Returns whether or not your index is currently sorted ascended.

Returns:

  • (Boolean)


105
106
107
# File 'lib/index_view/implementation.rb', line 105

def ascending?
  sort_direction == ASC
end

#columnsObject

returns a collection of the IndexView::Column objects that were added through the column method



95
96
97
# File 'lib/index_view/implementation.rb', line 95

def columns
  self.class.columns
end

#descending?Boolean

Returns whether or not your index is currently sorted descended.

Returns:

  • (Boolean)


110
111
112
# File 'lib/index_view/implementation.rb', line 110

def descending?
  !ascending?
end

#fields_for_searchObject



122
123
124
# File 'lib/index_view/implementation.rb', line 122

def fields_for_search
  self.class.fields_for_search
end

#find(selector, options = {}) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/index_view/implementation.rb', line 24

def find(selector, options={})
  case selector
  when :first, :last, :all
    target_class.find(selector, find_options.merge(options))
  else
    target_class.find(selector)
  end
end

#find_optionsObject



41
42
43
44
45
46
47
# File 'lib/index_view/implementation.rb', line 41

def find_options
  {
    :from       => table_name.to_s,
    :order      => sort,
    :conditions => conditions_sql
  }
end

#first(*args) ⇒ Object



37
38
39
# File 'lib/index_view/implementation.rb', line 37

def first(*args)
  find(:first, *args)
end

#initialize(params = { }) ⇒ Object

IndexView objects are initialized with the params of a request. See README.rdoc



13
14
15
# File 'lib/index_view/implementation.rb', line 13

def initialize(params = { })
  @params = params
end

#opposite_sort_directionObject



89
90
91
# File 'lib/index_view/implementation.rb', line 89

def opposite_sort_direction
  ascending? ? DESC : ASC
end

#paginateObject

returns a paginated set of the IndexView’s target_class To customize how the objects are paginated - redefine pagination_options in your class



20
21
22
# File 'lib/index_view/implementation.rb', line 20

def paginate
  target_class.paginate(pagination_options)
end

#pagination_optionsObject

Returns a hash of options used to paginate your IndexView’s target_class. You can overwrite this in your class to customize pagination.



51
52
53
54
55
56
57
58
59
# File 'lib/index_view/implementation.rb', line 51

def pagination_options
  {
    :from       => table_name.to_s,
    :conditions => conditions_sql,
    :order      => sort,
    :page       => @params[:page],
    :per_page   => per_page
  }
end

#search_termObject



61
62
63
# File 'lib/index_view/implementation.rb', line 61

def search_term
  @params[:search]
end

#search_term?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/index_view/implementation.rb', line 65

def search_term?
  search_term && !search_term.blank? ? true : false
end

#sortObject



69
70
71
# File 'lib/index_view/implementation.rb', line 69

def sort
  "#{sort_term} #{sort_direction}" + (!secondary_sort_term.blank? ? ", #{secondary_sort_term} #{sort_direction}" : '')
end

#sort_directionObject



81
82
83
84
85
86
87
# File 'lib/index_view/implementation.rb', line 81

def sort_direction
  if SORT_DIRECTIONS.include?(given_sort_direction)
    given_sort_direction
  else
    raise IndexView::InvalidSort, "#{given_sort_direction} is not a valid sort direction"
  end
end

#sort_termObject



73
74
75
76
77
78
79
# File 'lib/index_view/implementation.rb', line 73

def sort_term
  if sort = @params[:sort]
    sort
  else
    default_sort_term.is_a?(Array) ? default_sort_term.join(", ") : default_sort_term
  end
end

#sorting?(column_name) ⇒ Boolean

Takes a column name and returns whether or not your index is currently sorted on that column.

Returns:

  • (Boolean)


100
101
102
# File 'lib/index_view/implementation.rb', line 100

def sorting?(column_name)
  sort_term.to_s == column_name.to_s
end

#stateObject



118
119
120
# File 'lib/index_view/implementation.rb', line 118

def state
  @params[:state]
end

#state?Boolean

Returns:

  • (Boolean)


114
115
116
# File 'lib/index_view/implementation.rb', line 114

def state?
  state && !state.blank? ? true : false
end

#table_nameObject



126
127
128
# File 'lib/index_view/implementation.rb', line 126

def table_name
  target_class.table_name.to_sym
end