leaf 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -8,6 +8,9 @@ A _really_ simple pagination library, heavily based on the agnostic branch of
8
8
  Leaf supports pagination for collections responding to +total_pages+,
9
9
  +per_page+, +previous_page+ and +total_entries+ in Sinatra views out of the box.
10
10
 
11
+ It currently supports two renderers: +Leaf::ViewHelpers::LinkRenderer+
12
+ and +Leaf::ViewHelpers::ListRenderer+
13
+
11
14
  == Installation
12
15
 
13
16
  gem install leaf
@@ -20,7 +23,8 @@ Leaf supports pagination for collections responding to +total_pages+,
20
23
 
21
24
  include Leaf::ViewHelpers::Base
22
25
 
23
- # Needed to paginate any array, you’ll probably use something else.
26
+ # Needed to paginate any array
27
+ # you’ll probably use something else.
24
28
  require 'leaf/array'
25
29
 
26
30
  get '/' do
@@ -28,13 +32,16 @@ Leaf supports pagination for collections responding to +total_pages+,
28
32
  array = ('a'..'z').to_a
29
33
 
30
34
  haml :index, :locals => {
31
- :collection => array.paginate({:page => page, :per_page => 5})
35
+ :collection => array.paginate({
36
+ :page => page,
37
+ :per_page => 5
38
+ })
32
39
  }
33
40
  end
34
41
 
35
42
  __END__
36
43
  @@ index
37
- = leaf(collection)
44
+ = leaf(collection, :renderer => Leaf::ViewHelpers::ListRenderer)
38
45
  %ul
39
46
  - collection.each do |letter|
40
47
  %li= letter
@@ -8,7 +8,7 @@
8
8
  module Leaf
9
9
  require 'leaf/version'
10
10
 
11
- # Load the helpers for sinatra
11
+ # Load the helpers for Sinatra
12
12
  if defined?(Sinatra)
13
13
  require 'leaf/view_helpers/sinatra'
14
14
  end
@@ -2,7 +2,7 @@ module Leaf
2
2
  module VERSION
3
3
  MAJOR = 0
4
4
  MINOR = 1
5
- TINY = 1
5
+ TINY = 2
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
@@ -46,13 +46,11 @@ module Leaf
46
46
 
47
47
  # get the renderer instance
48
48
  renderer = case options[:renderer]
49
- when String
50
- options[:renderer].constantize.new
51
- when Class
52
- options[:renderer].new
53
- else
54
- options[:renderer]
49
+ when String then options[:renderer].constantize.new
50
+ when Class then options[:renderer].new
51
+ else options[:renderer]
55
52
  end
53
+
56
54
  # render HTML for pagination
57
55
  renderer.prepare collection, options, self
58
56
  renderer.to_html
@@ -0,0 +1,39 @@
1
+ require 'leaf/view_helpers/link_renderer'
2
+
3
+ module Leaf
4
+ module ViewHelpers
5
+ class ListRenderer < LinkRenderer
6
+ def to_html
7
+ html = pagination.map do |item|
8
+ "\n " + tag(:li, (item.is_a?(Fixnum) ?
9
+ page_number(item) :
10
+ send(item)))
11
+ end.join(@options[:separator])
12
+
13
+ @options[:container] ? html_container(html) : html
14
+ end
15
+
16
+ def previous_or_next_page(page, text, classname)
17
+ if page
18
+ link(tag(:span, text), page, :class => classname)
19
+ else
20
+ tag(:span, tag(:span, text), :class => classname + ' disabled')
21
+ end
22
+ end
23
+
24
+ def html_container(html)
25
+ tag(:div, "\n " +
26
+ tag(:ul, html + "\n ") + "\n", container_attributes) + "\n"
27
+ end
28
+
29
+ private
30
+ def page_number(page)
31
+ unless page == current_page
32
+ link(tag(:span, page), page, :rel => rel_value(page))
33
+ else
34
+ tag(:em, tag(:span, page))
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -1,5 +1,6 @@
1
1
  require 'leaf/view_helpers/base'
2
2
  require 'leaf/view_helpers/link_renderer'
3
+ require 'leaf/view_helpers/list_renderer'
3
4
 
4
5
  Leaf::ViewHelpers::LinkRenderer.class_eval do
5
6
  protected
@@ -16,4 +17,4 @@ Leaf::ViewHelpers::LinkRenderer.class_eval do
16
17
  end
17
18
  end
18
19
  end
19
- end
20
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: leaf
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 31
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 1
10
- version: 0.1.1
9
+ - 2
10
+ version: 0.1.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Peter Hellberg
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-06-28 00:00:00 +02:00
18
+ date: 2010-06-29 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -54,6 +54,7 @@ files:
54
54
  - lib/leaf/view_helpers/base.rb
55
55
  - lib/leaf/view_helpers/link_renderer.rb
56
56
  - lib/leaf/view_helpers/link_renderer_base.rb
57
+ - lib/leaf/view_helpers/list_renderer.rb
57
58
  - lib/leaf/view_helpers/sinatra.rb
58
59
  - lib/leaf/view_helpers.rb
59
60
  - lib/leaf.rb