generate-column-widths 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +22 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +193 -0
- data/Rakefile +2 -0
- data/generate-column-widths.gemspec +25 -0
- data/gitignore +5 -0
- data/lib/generate-column-widths.rb +20 -0
- data/lib/generate-column-widths/version.rb +3 -0
- metadata +84 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: aeaae8a97425684bcaaa894651131a3f6fe93d28
|
4
|
+
data.tar.gz: fe60d18972ef4917c5e1a6c539e4b40ab71ddace
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 81218fd6c200cb01573b4e3f797a77d479723b5f0b3e84967ededb6fa450436454c0c5e39c7912be4b8e475025c42775212953ed31189f82b89aa517136a0e0e
|
7
|
+
data.tar.gz: 9c418a888ffca67054c6b950e8fd09746857e527c1f2dadcf4ef118fd2a84d49833c35a7e84de7aeac031ebbc693d7dfd37d4cd3af6e06ccb11e082baf13be2a
|
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 'Liane Allen'
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,193 @@
|
|
1
|
+
# GenerateColumnWidths
|
2
|
+
|
3
|
+
This gem allows you to calculate the number of columns to span in a css grid
|
4
|
+
when your site may have a variable number of columns of data to display.
|
5
|
+
|
6
|
+
It allows you to generate the number of columns you need, and to have them
|
7
|
+
automatically be the correct widths to fit your page, based on the data you have
|
8
|
+
to display. You don't need to know a priori how many columns your data will
|
9
|
+
produce, and you don't need to fill your html with a bunch of centering divs to
|
10
|
+
try to make it look ok.
|
11
|
+
|
12
|
+
It also allows you to accommodate a situation when the columns being generated
|
13
|
+
need to fit alongside columns that are already on the page.
|
14
|
+
|
15
|
+
You use the generated variable to create columns automatically in your views.
|
16
|
+
For example, in this snippet, which generates a 4 column layout for screen sizes
|
17
|
+
small and above (a Bootstrap concept):
|
18
|
+
|
19
|
+
<% @your_collection.each do |item| %>
|
20
|
+
<div class="col-sm-3">
|
21
|
+
...
|
22
|
+
<% end %>
|
23
|
+
|
24
|
+
The variable generated by the gem replaces the "3" in the col-sm-3 with the
|
25
|
+
variable generated by the gem's method. As your view iterates through the
|
26
|
+
items in the collection and renders them in the view, it will generate only the
|
27
|
+
number of columns you need for the data you have. The columns will
|
28
|
+
automatically be the right width to accommodate your data. If you have fewer
|
29
|
+
than expected, your columns won't all be clumped together to one side- and you
|
30
|
+
can accomplish that without having to add a bunch of centering divs to wrap the
|
31
|
+
content.
|
32
|
+
|
33
|
+
This doc is largely implementation oriented. If you need a bit more of a plain
|
34
|
+
English description of what it does, check out my blog post:
|
35
|
+
[lianeallen.com/home/2014/09/module-to-auto-generate-column-widths-in-a-grid-system/](http://www.lianeallen.com/home/2014/09/module-to-auto-generate-column-widths-in-a-grid-system/)
|
36
|
+
|
37
|
+
## Installation
|
38
|
+
|
39
|
+
Add this line to your application's Gemfile:
|
40
|
+
|
41
|
+
gem 'generate-column-widths'
|
42
|
+
|
43
|
+
And then execute:
|
44
|
+
|
45
|
+
$ bundle
|
46
|
+
|
47
|
+
Or install it yourself as:
|
48
|
+
|
49
|
+
$ gem install generate-column-widths
|
50
|
+
|
51
|
+
## Usage
|
52
|
+
Once it's installed, it's easy to use:
|
53
|
+
|
54
|
+
###1. Include the Module in the Model that Needs It
|
55
|
+
|
56
|
+
In the model whose views will need auto-generated column widths:
|
57
|
+
|
58
|
+
include GenerateColumnWidths
|
59
|
+
|
60
|
+
#### Example
|
61
|
+
Since I'm using this for views that contain lists of project categories, I put
|
62
|
+
it in my Category model, category.rb:
|
63
|
+
|
64
|
+
class Category < ActiveRecord::Base
|
65
|
+
include GenerateColumnWidths
|
66
|
+
...
|
67
|
+
end
|
68
|
+
|
69
|
+
###2. Determine How Many Columns are in Your Data
|
70
|
+
From the controller where the view is generated, calculate the number of columns
|
71
|
+
you need.
|
72
|
+
|
73
|
+
####Example:
|
74
|
+
In my case, I needed to get a count of the number of meta-category headings I'd
|
75
|
+
have in my view by finding out how many of those meta-categories contained any
|
76
|
+
associated projects:
|
77
|
+
|
78
|
+
@category_types = # code to find which meta categories contain projects
|
79
|
+
@category_types_count = category_types.count
|
80
|
+
|
81
|
+
###3. Call the Generator
|
82
|
+
In your controller, call the generator.
|
83
|
+
|
84
|
+
Simply call the generate_widths method with your new variable. Call it via the
|
85
|
+
model into which you placed it. In my case, that's Category.
|
86
|
+
|
87
|
+
This is the code in my categories_controller.
|
88
|
+
|
89
|
+
@column_width = Category.new.generate_widths(
|
90
|
+
@category_types_count
|
91
|
+
)
|
92
|
+
|
93
|
+
I use @column_width in the Bootstrap div in my categories index view (see below).
|
94
|
+
|
95
|
+
###5. Use it in Your View
|
96
|
+
Replace the number in the div that defines the column span in your view.
|
97
|
+
Here's what it looks like in a Bootstrap column div for small views and higher:
|
98
|
+
|
99
|
+
<% @category_types.each do |type| %>
|
100
|
+
<div class="col-sm-<%= @column_width %>">
|
101
|
+
...
|
102
|
+
<% end %>
|
103
|
+
|
104
|
+
Be sure to put the div inside the iterator, or you're going to end up with just one
|
105
|
+
narrow column.
|
106
|
+
|
107
|
+
###You Can Change the Defaults
|
108
|
+
The generator has several defaults built in. It assumes you want a maximum of 4
|
109
|
+
columns in your view, using a 12 column Bootstrap3 grid. You can change the
|
110
|
+
defaults by specifying a different value for any or all of the optional variables.
|
111
|
+
|
112
|
+
You need only include those options you want to change. Any option you don't
|
113
|
+
include will automatically use the default.
|
114
|
+
|
115
|
+
####Option: total_grid_columns
|
116
|
+
The number of columns in your grid system. Bootstrap3 uses 12, others may use 9
|
117
|
+
or some other number.
|
118
|
+
|
119
|
+
Default:
|
120
|
+
|
121
|
+
total_grid_columns: 12
|
122
|
+
|
123
|
+
####Option: existing_columns
|
124
|
+
The number of columns already in the view, that will share a row with your new
|
125
|
+
columns. In the vast majority of cases, you won't need to change this, but it's
|
126
|
+
here in case you do.
|
127
|
+
|
128
|
+
Default:
|
129
|
+
|
130
|
+
existing_columns: 0
|
131
|
+
|
132
|
+
####Option: min_column_span
|
133
|
+
The minimum span you want to use. This is one of those counter-intuitive things
|
134
|
+
in Bootstrap. It's not the number of columns you want, it's:
|
135
|
+
|
136
|
+
How many of the 12 columns in the grid do you want this column to span?
|
137
|
+
|
138
|
+
So, you divide the total number of columns in the grid by this number to get
|
139
|
+
the number of columns you're trying to span.
|
140
|
+
|
141
|
+
The result is how many columns you'll end up with in your row, before Bootstrap
|
142
|
+
wraps to a new line to display the next set of columns.
|
143
|
+
|
144
|
+
The default is to span 3 columns, at a minimum, which means (12 / 3 = 4) you'll
|
145
|
+
get a maximum of 4 columns in the view.
|
146
|
+
|
147
|
+
Default:
|
148
|
+
|
149
|
+
min_column_span: 3
|
150
|
+
|
151
|
+
Unless you're fond of seeing divide by zero errors, don't set this to zero.
|
152
|
+
|
153
|
+
####Option: max_column_span
|
154
|
+
This is the maximum number of columns you want to span. It's the opposite of the
|
155
|
+
above. The default is 12, so divide 12/12, and you get ... 1. That's the minimum
|
156
|
+
number of columns that will be shown.
|
157
|
+
|
158
|
+
Default:
|
159
|
+
|
160
|
+
max_column_span: 12
|
161
|
+
|
162
|
+
Unless you're fond of seeing divide by zero errors, don't set this to zero.
|
163
|
+
|
164
|
+
####Example 1
|
165
|
+
On one page, I need to accomodate an extra column of data that's generated
|
166
|
+
elsewhere. So my categories columns will appear in the same row as an extra
|
167
|
+
column. To deal with this, I can call:
|
168
|
+
|
169
|
+
@column_width = Category.new.generate_widths(
|
170
|
+
@category_types_count,
|
171
|
+
existing_columns: 1
|
172
|
+
)
|
173
|
+
|
174
|
+
####Example 2
|
175
|
+
Alternatively, if I were using a 9 grid, instead of a 12 grid, I could specify:
|
176
|
+
|
177
|
+
@column_width = Category.new.generate_widths(
|
178
|
+
@category_types_count,
|
179
|
+
total_grid_columns: 9,
|
180
|
+
max_column_span: 9
|
181
|
+
)
|
182
|
+
|
183
|
+
In this case, the default min_column_span (3) would be divided into 9, giving me a
|
184
|
+
maximum of 3 columns in my view before Bootstrap wraps to a new line for
|
185
|
+
additional columns.
|
186
|
+
|
187
|
+
## Contributing
|
188
|
+
|
189
|
+
1. Fork it ( https://github.com/[my-github-username]/generate-column-widths/fork )
|
190
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
191
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
192
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
193
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'generate-column-widths/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "generate-column-widths"
|
8
|
+
spec.version = GenerateColumnWidths::VERSION
|
9
|
+
spec.authors = ["'Liane Allen'"]
|
10
|
+
spec.email = ["'github@lianeallen.com'"]
|
11
|
+
spec.summary = %q{Generate the span value to use with a css grid system.}
|
12
|
+
spec.description = %q{Automatically generates the span value to use when
|
13
|
+
your view may have a variable number of columns,
|
14
|
+
and you're using a css grid system.}
|
15
|
+
spec.homepage = "http://github.com/liantics/generate-column-widths"
|
16
|
+
spec.license = "MIT"
|
17
|
+
|
18
|
+
spec.files = `git ls-files -z`.split("\x0")
|
19
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
20
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
24
|
+
spec.add_development_dependency "rake"
|
25
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require "generate-column-widths/version"
|
2
|
+
|
3
|
+
module GenerateColumnWidths
|
4
|
+
def generate_widths(new_columns,
|
5
|
+
total_grid_columns: 12,
|
6
|
+
existing_columns: 0,
|
7
|
+
min_column_span: 3,
|
8
|
+
max_column_span: 12)
|
9
|
+
columns_in_row = new_columns + existing_columns
|
10
|
+
span = (total_grid_columns / columns_in_row).floor
|
11
|
+
|
12
|
+
if span < min_column_span
|
13
|
+
span = min_column_span
|
14
|
+
elsif span > max_column_span
|
15
|
+
span = max_column_span
|
16
|
+
end
|
17
|
+
|
18
|
+
span
|
19
|
+
end
|
20
|
+
end
|
metadata
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: generate-column-widths
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- "'Liane Allen'"
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-09-23 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.6'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: |-
|
42
|
+
Automatically generates the span value to use when
|
43
|
+
your view may have a variable number of columns,
|
44
|
+
and you're using a css grid system.
|
45
|
+
email:
|
46
|
+
- "'github@lianeallen.com'"
|
47
|
+
executables: []
|
48
|
+
extensions: []
|
49
|
+
extra_rdoc_files: []
|
50
|
+
files:
|
51
|
+
- ".gitignore"
|
52
|
+
- Gemfile
|
53
|
+
- LICENSE.txt
|
54
|
+
- README.md
|
55
|
+
- Rakefile
|
56
|
+
- generate-column-widths.gemspec
|
57
|
+
- gitignore
|
58
|
+
- lib/generate-column-widths.rb
|
59
|
+
- lib/generate-column-widths/version.rb
|
60
|
+
homepage: http://github.com/liantics/generate-column-widths
|
61
|
+
licenses:
|
62
|
+
- MIT
|
63
|
+
metadata: {}
|
64
|
+
post_install_message:
|
65
|
+
rdoc_options: []
|
66
|
+
require_paths:
|
67
|
+
- lib
|
68
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
requirements: []
|
79
|
+
rubyforge_project:
|
80
|
+
rubygems_version: 2.2.2
|
81
|
+
signing_key:
|
82
|
+
specification_version: 4
|
83
|
+
summary: Generate the span value to use with a css grid system.
|
84
|
+
test_files: []
|