listy 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/.rvmrc +2 -0
- data/README.md +54 -7
- data/lib/listy/version.rb +1 -1
- data/lib/listy/view_helpers.rb +33 -23
- metadata +2 -1
data/.rvmrc
ADDED
data/README.md
CHANGED
@@ -29,28 +29,75 @@ Listy also includes some Javascript assets, so be sure to add the following line
|
|
29
29
|
|
30
30
|
There are a couple of helper methods including:
|
31
31
|
|
32
|
-
|
32
|
+
### listy_links
|
33
33
|
|
34
|
-
|
34
|
+
listy_links(collection, display_method_name, options={})
|
35
35
|
|
36
|
-
|
36
|
+
This method creates a simple unordered list of the elements in the collection. This is suitable for Ruby on Rails models.
|
37
|
+
|
38
|
+
Options you can pass in include
|
39
|
+
|
40
|
+
* :css_class - The class that is applied to the resulting ul list element
|
41
|
+
* :empty_message - The message that is displayed if the collection is empty
|
42
|
+
* :show_more - If set to true, the list will display only the first 10 elements, and hide the rest and create a "Show More" link. Be sure to have `//= require listy` in your application.js for the "Show More" link to work.
|
43
|
+
* :show_more_limit - If you want more that the first 10 elements to show when show_more is set to true, then specify that limit here.
|
44
|
+
|
45
|
+
Examples
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
<%= listy_links BlogPost.all, :title %>
|
49
|
+
|
50
|
+
<%= listy_links User.all, :email, :show_more => true %>
|
51
|
+
|
52
|
+
<%= listy_links Product.all, :part_number, :show_more => true, :show_more_limit => 100, :css_class => "products-list" %>
|
53
|
+
```
|
54
|
+
### multi_column_listy_links
|
55
|
+
|
56
|
+
multi_column_listy_links(collection, display_method_name, number_of_columns, options={})
|
37
57
|
|
38
58
|
This does the same thing as listy_links but presents it in the number of columns you specify.
|
39
59
|
|
40
|
-
|
60
|
+
Options you can pass in include:
|
61
|
+
|
62
|
+
* :css_class - The class that is applied to the resulting ul list elements in each column
|
63
|
+
* :empty_message - The message that is displayed if the collection is empty
|
64
|
+
|
65
|
+
Examples
|
66
|
+
|
67
|
+
```ruby
|
68
|
+
<%= listy_links BlogPost.all, :title %>
|
69
|
+
|
70
|
+
<%= listy_links User.all, :email, :show_more => true %>
|
71
|
+
|
72
|
+
<%= listy_links Product.all, :part_number, :show_more => true, :show_more_limit => 100, :css_class => "products-list" %>
|
73
|
+
```
|
74
|
+
|
75
|
+
### listy_tree
|
76
|
+
|
77
|
+
listy_tree(collection, spec, options={})
|
41
78
|
|
42
79
|
This method is for creating a nested tree of unordered lists for a collection with nested collections. This is suitable for Rails models that have has_many relationships.
|
80
|
+
The spec is basically instructions on how to create the nested tree. It is a nested hash where each child specifies how to create the nested list
|
43
81
|
|
44
82
|
Example let's say we have the following
|
45
83
|
|
46
84
|
* Country which has_many States
|
47
85
|
* State which has_many Cities
|
48
86
|
|
49
|
-
|
87
|
+
Examples:
|
50
88
|
|
51
|
-
|
89
|
+
```ruby
|
90
|
+
listy_tree(Country.all,
|
91
|
+
{ :display_method_name => :name,
|
92
|
+
:children => :states,
|
93
|
+
:child => { :display_method_name => :name,
|
94
|
+
:children => :cities,
|
95
|
+
:child => { :display_method_name => :name }
|
96
|
+
}
|
97
|
+
})
|
98
|
+
```
|
52
99
|
|
53
|
-
Be sure that the inner most child of the spec has :children => nil. This will stop the nesting.
|
100
|
+
Be sure that the inner most child of the spec has :children => nil (or just leave it undefined). This will stop the nesting.
|
54
101
|
|
55
102
|
|
56
103
|
## Contributing
|
data/lib/listy/version.rb
CHANGED
data/lib/listy/view_helpers.rb
CHANGED
@@ -1,54 +1,49 @@
|
|
1
1
|
module Listy
|
2
2
|
module ViewHelpers
|
3
3
|
|
4
|
-
def listy_tree(collection, spec,
|
4
|
+
def listy_tree(collection, spec, options={})
|
5
5
|
if collection.present?
|
6
6
|
html = "<div class='listy-tree'>" + create_listy_tree(collection, spec, "", 0) + "</div>"
|
7
7
|
else
|
8
8
|
html = "There are no entries in this tree."
|
9
|
-
html = empty_message if !empty_message.nil?
|
9
|
+
html = options[:empty_message] if !options[:empty_message].nil?
|
10
10
|
end
|
11
11
|
raw html
|
12
12
|
end
|
13
13
|
|
14
|
-
|
15
|
-
html += "<ul class='listy-tree-level-#{level}'>"
|
16
|
-
|
17
|
-
collection.each do |element|
|
18
|
-
html += "<li>"
|
19
|
-
html += "<div class='listy-tree-list-header'>#{element.try(spec[:display_method_name])}</div>"
|
20
|
-
html = create_listy_tree(element.try(spec[:children]), spec[:child], html, level+1) if !spec[:children].nil?
|
21
|
-
html += "</li>"
|
22
|
-
end
|
23
|
-
|
24
|
-
html += "</ul>"
|
25
|
-
end
|
14
|
+
|
26
15
|
|
27
|
-
def listy_links(collection, display_method_name,
|
16
|
+
def listy_links(collection, display_method_name, options={})
|
28
17
|
if collection.present?
|
29
|
-
html = "<ul class='" + css_class + "'>"
|
30
|
-
|
18
|
+
html = "<ul class='" + options[:css_class] + "'>"
|
19
|
+
show_more_limit_reached = false
|
20
|
+
|
21
|
+
show_more_limit = options[:show_more_limit] || 10 if options[:show_more]
|
22
|
+
|
31
23
|
collection.each_with_index do |element, index|
|
32
|
-
|
24
|
+
|
25
|
+
if options[:show_more] && (index+1) > show_more_limit && !show_more_limit_reached
|
33
26
|
html += "<div class='listy-show-more-list' style='display:none'>"
|
34
|
-
|
27
|
+
show_more_limit_reached = true
|
35
28
|
end
|
36
29
|
html += "<li>" + link_to(element.try(display_method_name), element) + "</li>"
|
37
30
|
end
|
38
|
-
|
31
|
+
|
32
|
+
if options[:show_more] && show_more_limit_reached
|
39
33
|
html += "</div>"
|
40
34
|
html += link_to("Show More", "#", :class => "listy-show-more-link button orange-button")
|
41
35
|
end
|
36
|
+
|
42
37
|
html += "</ul>"
|
43
38
|
else
|
44
39
|
html = "There are no entries in this list."
|
45
|
-
html = empty_message if !empty_message.nil?
|
40
|
+
html = options[:empty_message] if !options[:empty_message].nil?
|
46
41
|
end
|
47
42
|
|
48
43
|
raw html
|
49
44
|
end
|
50
45
|
|
51
|
-
def multi_column_listy_links(collection, display_method_name,
|
46
|
+
def multi_column_listy_links(collection, display_method_name, number_of_columns, options={})
|
52
47
|
html = ""
|
53
48
|
if collection.present?
|
54
49
|
number_of_entries_per_column = collection.size/number_of_columns
|
@@ -61,7 +56,7 @@ module Listy
|
|
61
56
|
end_index = (i+1) * number_of_entries_per_column
|
62
57
|
end_index = collection.size if end_index > collection.size
|
63
58
|
|
64
|
-
html +=
|
59
|
+
html += listy_links(collection[start_index..end_index], display_method_name, options)
|
65
60
|
|
66
61
|
html += "</div>"
|
67
62
|
end
|
@@ -70,10 +65,25 @@ module Listy
|
|
70
65
|
|
71
66
|
else
|
72
67
|
html = "There are no entries in this list."
|
68
|
+
html = options[:empty_message] if !options[:empty_message].nil?
|
73
69
|
end
|
74
70
|
|
75
71
|
raw html
|
76
72
|
end
|
73
|
+
|
74
|
+
private
|
75
|
+
def create_listy_tree(collection, spec, html, level)
|
76
|
+
html += "<ul class='listy-tree-level-#{level}'>"
|
77
|
+
|
78
|
+
collection.each do |element|
|
79
|
+
html += "<li>"
|
80
|
+
html += "<div class='listy-tree-list-header'>#{element.try(spec[:display_method_name])}</div>"
|
81
|
+
html = create_listy_tree(element.try(spec[:children]), spec[:child], html, level+1) if !spec[:children].nil?
|
82
|
+
html += "</li>"
|
83
|
+
end
|
84
|
+
|
85
|
+
html += "</ul>"
|
86
|
+
end
|
77
87
|
|
78
88
|
end
|
79
89
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: listy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -19,6 +19,7 @@ extensions: []
|
|
19
19
|
extra_rdoc_files: []
|
20
20
|
files:
|
21
21
|
- .gitignore
|
22
|
+
- .rvmrc
|
22
23
|
- Gemfile
|
23
24
|
- LICENSE
|
24
25
|
- README.md
|