listy 0.0.5 → 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +65 -18
- data/lib/listy/version.rb +1 -1
- data/lib/listy/view_helpers.rb +14 -14
- metadata +1 -1
data/README.md
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
# Listy
|
2
2
|
|
3
3
|
Listy is a Gem that contains a bunch of view helpers for use in Ruby on Rails to easily create different types of lists.
|
4
|
+
|
4
5
|
I apologize for all the updates happening in 0.0.X. This started out as a test gem project with very bad code, and I'm refactoring and updating the code pretty frequently.
|
5
6
|
I will do my best to keep this document up to date with the versions.
|
6
7
|
|
@@ -9,6 +10,15 @@ I will do my best to keep this document up to date with the versions.
|
|
9
10
|
Listy is only compatible with Rails 3.1 and above.
|
10
11
|
It leverages both the Asset Pipeline and the JQuery library.
|
11
12
|
|
13
|
+
So either manually include your own version of JQuery, or ensure the bundled version is specified in your `app/assets/javascripts/application.js`
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
//= require jquery
|
17
|
+
//= require jquery_ujs
|
18
|
+
```
|
19
|
+
|
20
|
+
If you don't plan on using any of the Hiding/Collapsing of the lists - you don't have to include the JQuery stuff.
|
21
|
+
|
12
22
|
## Installation
|
13
23
|
|
14
24
|
Add this line to your application's Gemfile:
|
@@ -27,74 +37,111 @@ Listy also includes some Javascript assets, so be sure to add the following line
|
|
27
37
|
|
28
38
|
//= require listy
|
29
39
|
|
40
|
+
If you don't plan on using any of the Hiding/Collapsing of the lists - you don't have to include this.
|
41
|
+
|
30
42
|
## Usage
|
31
43
|
|
32
44
|
There are a couple of helper methods including:
|
33
45
|
|
34
46
|
### listy_links
|
35
47
|
|
36
|
-
listy_links(collection,
|
48
|
+
listy_links(collection, options={})
|
49
|
+
|
50
|
+
This method creates a simple unordered list of links of the elements in the collection. This is suitable for Ruby on Rails models.
|
37
51
|
|
38
|
-
|
52
|
+
* collection - Should be an ActiveRecord collection.
|
39
53
|
|
40
54
|
Options you can pass in include
|
41
55
|
|
56
|
+
* display_method - Symbol of method to be called on each record to generate what is displayed in the list. If you set is not specified, to_s will be called on the element to generate the display string.
|
42
57
|
* :css_class - The class that is applied to the resulting ul list element
|
43
58
|
* :empty_message - The message that is displayed if the collection is empty
|
44
|
-
* :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.
|
59
|
+
* :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. Show more functionality requires JQuery and list.js - please read above.
|
45
60
|
* :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.
|
46
61
|
|
47
|
-
|
62
|
+
#### Example Usage:
|
48
63
|
|
49
64
|
```ruby
|
50
|
-
<%= listy_links BlogPost.all
|
65
|
+
<%= listy_links BlogPost.all %> <!-- probably not that useful -->
|
66
|
+
|
67
|
+
<%= listy_links BlogPost.all, :display_method => :title %>
|
51
68
|
|
52
|
-
<%= listy_links User.all, :email, :show_more => true %>
|
69
|
+
<%= listy_links User.all, :display_method => :email, :show_more => true %>
|
53
70
|
|
54
|
-
<%= listy_links Product.all, :part_number, :show_more => true, :show_more_limit => 100, :css_class => "products-list" %>
|
71
|
+
<%= listy_links Product.all, :display_method => :part_number, :show_more => true, :show_more_limit => 100, :css_class => "products-list" %>
|
55
72
|
```
|
56
73
|
### multi_column_listy_links
|
57
74
|
|
58
|
-
multi_column_listy_links(collection,
|
75
|
+
multi_column_listy_links(collection, number_of_columns, options={})
|
59
76
|
|
60
77
|
This does the same thing as listy_links but presents it in the number of columns you specify.
|
61
78
|
|
79
|
+
* collection - Should be an ActiveRecord collection.
|
80
|
+
* number_of_columns - The number of columns you want the list split up into.
|
81
|
+
|
62
82
|
Options you can pass in include:
|
63
83
|
|
84
|
+
* display_method - Symbol of method to be called on each record to generate what is displayed in the list. If you set is not specified, to_s will be called on the element to generate the display string.
|
64
85
|
* :css_class - The class that is applied to the resulting ul list elements in each column
|
65
86
|
* :empty_message - The message that is displayed if the collection is empty
|
66
87
|
|
67
|
-
|
88
|
+
#### Example Usage:
|
68
89
|
|
69
90
|
```ruby
|
70
|
-
<%=
|
91
|
+
<%= multi_column_listy_links BlogPost.all, 3, :display_method => :title %>
|
71
92
|
|
72
|
-
<%=
|
93
|
+
<%= multi_column_listy_links User.all, 2, :display_method => :email %>
|
73
94
|
|
74
|
-
<%=
|
95
|
+
<%= multi_column_listy_links Product.all, 4, :display_method => :part_number, :css_class => "products-list", :empty_message => "There are no products" %>
|
75
96
|
```
|
76
97
|
|
77
98
|
### listy_tree
|
78
99
|
|
79
100
|
listy_tree(collection, spec, options={})
|
80
101
|
|
81
|
-
This method is for creating a nested tree of unordered lists for a collection with nested collections.
|
102
|
+
This method is for creating a nested tree of unordered lists for a collection with nested collections.
|
103
|
+
This is suitable for Rails models that have has_many relationships. The lists will be collapsable if you click on the parent elements (requires JQuery and listy.js, please read above).
|
104
|
+
|
82
105
|
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
|
83
106
|
|
84
|
-
|
107
|
+
|
108
|
+
#### Spec Explanation
|
109
|
+
|
110
|
+
The spec is a nested hash that contains the following elements:
|
111
|
+
|
112
|
+
* display_method - Symbol of method to be called on each record to generate what is displayed in the list. If you set is not specified, to_s will be called on the element to generate the display string.
|
113
|
+
* :children - The name of the method (typically the has_many relationship) to call to get the nested collection (leave unspecified or nil if this is the leaf node)
|
114
|
+
* :child - The spec for the elements of the children (leave unspecified or nil if this is the leaf node)
|
115
|
+
|
116
|
+
Here's a sample spec
|
117
|
+
|
118
|
+
Let's say we have the following
|
85
119
|
|
86
120
|
* Country which has_many States
|
87
121
|
* State which has_many Cities
|
88
122
|
|
89
|
-
|
123
|
+
|
124
|
+
```ruby
|
125
|
+
spec = { :display_method => :name,
|
126
|
+
:children => :states,
|
127
|
+
:child => { :display_method => :name,
|
128
|
+
:children => :cities,
|
129
|
+
:child => { :display_method => :name }
|
130
|
+
}
|
131
|
+
}
|
132
|
+
```
|
133
|
+
|
134
|
+
|
135
|
+
|
136
|
+
#### Example Usage:
|
90
137
|
|
91
138
|
```ruby
|
92
139
|
listy_tree(Country.all,
|
93
|
-
{ :
|
140
|
+
{ :display_method => :name,
|
94
141
|
:children => :states,
|
95
|
-
:child => { :
|
142
|
+
:child => { :display_method => :name,
|
96
143
|
:children => :cities,
|
97
|
-
:child => { :
|
144
|
+
:child => { :display_method => :name }
|
98
145
|
}
|
99
146
|
})
|
100
147
|
```
|
data/lib/listy/version.rb
CHANGED
data/lib/listy/view_helpers.rb
CHANGED
@@ -5,17 +5,17 @@ module Listy
|
|
5
5
|
if collection.present?
|
6
6
|
html = "<div class='listy-tree'>" + create_listy_tree(collection, spec, "", 0) + "</div>"
|
7
7
|
else
|
8
|
-
html = "There are no entries in this
|
9
|
-
html = options[:empty_message] if !options[:empty_message].nil?
|
8
|
+
html = options[:empty_message] || "There are no entries in this list."
|
10
9
|
end
|
11
10
|
raw html
|
12
11
|
end
|
13
12
|
|
14
13
|
|
15
14
|
|
16
|
-
def listy_links(collection,
|
15
|
+
def listy_links(collection, options={})
|
17
16
|
if collection.present?
|
18
|
-
|
17
|
+
css_class = options[:css_class] || ""
|
18
|
+
html = "<ul class='" + css_class + "'>"
|
19
19
|
show_more_limit_reached = false
|
20
20
|
|
21
21
|
show_more_limit = options[:show_more_limit] || 10 if options[:show_more]
|
@@ -26,7 +26,8 @@ module Listy
|
|
26
26
|
html += "<div class='listy-show-more-list' style='display:none'>"
|
27
27
|
show_more_limit_reached = true
|
28
28
|
end
|
29
|
-
|
29
|
+
display = options[:display_method].nil? ? element.to_s : element.try(options[:display_method])
|
30
|
+
html += "<li>" + link_to(display, element) + "</li>"
|
30
31
|
end
|
31
32
|
|
32
33
|
if options[:show_more] && show_more_limit_reached
|
@@ -36,27 +37,26 @@ module Listy
|
|
36
37
|
|
37
38
|
html += "</ul>"
|
38
39
|
else
|
39
|
-
html = "There are no entries in this list."
|
40
|
-
html = options[:empty_message] if !options[:empty_message].nil?
|
40
|
+
html = options[:empty_message] || "There are no entries in this list."
|
41
41
|
end
|
42
42
|
|
43
43
|
raw html
|
44
44
|
end
|
45
45
|
|
46
|
-
def multi_column_listy_links(collection,
|
46
|
+
def multi_column_listy_links(collection, number_of_columns, options={})
|
47
47
|
html = ""
|
48
48
|
if collection.present?
|
49
|
-
number_of_entries_per_column = collection.size/number_of_columns
|
49
|
+
number_of_entries_per_column = (Float(collection.size)/Float(number_of_columns)).ceil
|
50
50
|
percentage_width_of_column = 100 / number_of_columns
|
51
51
|
Rails.logger.info(collection.size.to_s + " " + number_of_entries_per_column.to_s)
|
52
52
|
(0..number_of_columns-1).each do |i|
|
53
53
|
html += "<div style='float:left;width:" + percentage_width_of_column.to_s + "%'>"
|
54
54
|
|
55
55
|
start_index = i * number_of_entries_per_column
|
56
|
-
end_index = (i+1) * number_of_entries_per_column
|
56
|
+
end_index = (i+1) * number_of_entries_per_column - 1
|
57
57
|
end_index = collection.size if end_index > collection.size
|
58
58
|
|
59
|
-
html += listy_links(collection[start_index..end_index],
|
59
|
+
html += listy_links(collection[start_index..end_index], options)
|
60
60
|
|
61
61
|
html += "</div>"
|
62
62
|
end
|
@@ -64,8 +64,7 @@ module Listy
|
|
64
64
|
html += "<div style='clear:both'></div>"
|
65
65
|
|
66
66
|
else
|
67
|
-
html = "There are no entries in this list."
|
68
|
-
html = options[:empty_message] if !options[:empty_message].nil?
|
67
|
+
html = options[:empty_message] || "There are no entries in this list."
|
69
68
|
end
|
70
69
|
|
71
70
|
raw html
|
@@ -77,7 +76,8 @@ module Listy
|
|
77
76
|
|
78
77
|
collection.each do |element|
|
79
78
|
html += "<li>"
|
80
|
-
|
79
|
+
display = spec[:display_method].nil? ? element.to_s : element.try(spec[:display_method])
|
80
|
+
html += "<div class='listy-tree-list-header'>#{display}</div>"
|
81
81
|
html = create_listy_tree(element.try(spec[:children]), spec[:child], html, level+1) if !spec[:children].nil?
|
82
82
|
html += "</li>"
|
83
83
|
end
|