Module: Zena::Use::Context::ViewMethods

Includes:
RubyLess
Defined in:
lib/zena/use/context.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.visitor_node_procObject

Dynamic resolution of the author class from the user prototype



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/zena/use/context.rb', line 7

def self.visitor_node_proc
  Proc.new do |h, r, s|
    res = {:method => 'visitor.node', :nil => true}
    if prototype  = visitor.prototype
      res[:class] = prototype.vclass
    else
      res[:class] = VirtualClass['Node']
    end
    res
  end
end

Instance Method Details

#group_array(list) ⇒ Object

Group an array of records by key.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/zena/use/context.rb', line 28

def group_array(list)
  return nil if list.empty?
  groups = []
  h = {}
  list.each do |e|
    key = yield(e)
    unless group_id = h[key]
      h[key] = group_id = groups.size
      groups << []
    end
    groups[group_id] << e
  end
  groups
end

#max_array(list) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/zena/use/context.rb', line 75

def max_array(list)
  list.flatten.min do |a,b|
    va = yield(a)
    vb = yield(b)
    if va && vb
      vb <=> va
    elsif vb
      1
    elsif va
      -1
    else
      0
    end
  end
end

#min_array(list) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/zena/use/context.rb', line 59

def min_array(list)
  list.flatten.min do |a,b|
    va = yield(a)
    vb = yield(b)
    if va && vb
      va <=> vb
    elsif va
      1
    elsif vb
      -1
    else
      0
    end
  end
end

#page_numbers(current, count, join_string = nil, max_count = nil) ⇒ Object

Enter page numbers context.

Parameters

  • current - current page number

  • count - total number of pages

  • join_string - (optional) string to use to join page numbers

  • max_count - (optional) maximum number of pages to display

  • &block - block to yield for each page number. Receives |page_number, join_string|.



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/zena/use/context.rb', line 109

def page_numbers(current, count, join_string = nil, max_count = nil)
  max_count ||= 10
  join_string ||= ''
  join_str = ''
  if count <= max_count
    1.upto(count) do |p|
      yield(p, join_str, p)
      join_str = join_string
    end
  else
    # only first pages (centered around current page)
    if current - (max_count/2) > 0
      max = current + (max_count/2)
    else
      max = max_count
    end

    if count > max
      finish = end_dots = max
    else
      finish = count
    end

    start  = [finish - max_count + 1,1].max
    if start > 1
      start_dots = start
    end

    start.upto(finish) do |p|
      yield(p, join_str, (p == start_dots || p == end_dots) ? '' : p.to_s)
      join_str = join_string
    end
  end
end

#sort_array(list) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/zena/use/context.rb', line 43

def sort_array(list)
  list.sort do |a,b|
    va = yield([a].flatten[0])
    vb = yield([b].flatten[0])
    if va && vb
      va <=> vb
    elsif va
      1
    elsif vb
      -1
    else
      0
    end
  end
end

#start_nodeObject

main node before ajax stuff (the one in browser url)



92
93
94
95
96
97
98
# File 'lib/zena/use/context.rb', line 92

def start_node
  @start_node ||= if params[:s]
    secure(Node) { Node.find_by_zip(params[:s]) }
  else
    @node
  end
end