define_columns 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.rdoc +67 -0
- data/Rakefile +45 -0
- data/VERSION +1 -0
- data/lib/define_columns.rb +27 -0
- data/lib/define_columns/table_column.rb +28 -0
- data/lib/define_columns/table_columns.rb +23 -0
- data/spec/define_columns_spec.rb +75 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +17 -0
- metadata +77 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Wong Liang Zan
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
= define_columns
|
2
|
+
|
3
|
+
Define Columns allows you to manipulate the content columns of your ActiveRecord model.
|
4
|
+
|
5
|
+
== Features
|
6
|
+
|
7
|
+
* Add virtual attributes as columns
|
8
|
+
* Hiding existing content columns
|
9
|
+
* Changes the output of the column
|
10
|
+
|
11
|
+
== Benefits
|
12
|
+
|
13
|
+
* Allows you to place your logic in the model instead of the view.
|
14
|
+
* Does not assume your table markup. You have full flexibility to change things
|
15
|
+
* No need to hand write the table columns for virtual attributes
|
16
|
+
|
17
|
+
== Installation
|
18
|
+
|
19
|
+
$ gem install define_columns
|
20
|
+
|
21
|
+
== Usage
|
22
|
+
|
23
|
+
# Person has content_columns [ "name", "address", "gender", "hobbies" ]
|
24
|
+
# Note that the names must be an exact match
|
25
|
+
class Person < ActiveRecord::Base
|
26
|
+
define_columns do |columns|
|
27
|
+
columns.hide %w(name) # takes in an array of column names
|
28
|
+
columns.add %w(favourite_color) # can add virtual attributes as columns
|
29
|
+
columns.show(:gender) do |gender| # returns "-Male-" for column.show(person)
|
30
|
+
"-#{gender}-"
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def favourite_color
|
35
|
+
red
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
# Person.table_columns returns [ "address", "gender", "hobbies", "favourite_color" ]
|
40
|
+
|
41
|
+
# In your view(in haml)
|
42
|
+
%table
|
43
|
+
%thead
|
44
|
+
%tr
|
45
|
+
- Person.table_columns.each do |column|
|
46
|
+
%th= column.header
|
47
|
+
|
48
|
+
- @persons.each do |person|
|
49
|
+
%tbody
|
50
|
+
- Person.table_columns.each do |column|
|
51
|
+
%td= column.show(person)
|
52
|
+
|
53
|
+
== To Note
|
54
|
+
|
55
|
+
The show block takes in an argument and a block.
|
56
|
+
|
57
|
+
columns.show(:gender) do |gender|
|
58
|
+
"-#{gender}-"
|
59
|
+
end
|
60
|
+
|
61
|
+
The argument :gender must match the column name. The block parameter 'gender' is equivalent to the result of @person.gender.
|
62
|
+
|
63
|
+
Not having a show block renders @person.gender
|
64
|
+
|
65
|
+
== Copyright
|
66
|
+
|
67
|
+
Copyright (c) 2010 Wong Liang Zan. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "define_columns"
|
8
|
+
gem.summary = %Q{Customizable content columns for Rails}
|
9
|
+
gem.description = %Q{define_columns allows you to add, hide, customize the output of your columns}
|
10
|
+
gem.email = "zan@liangzan.net"
|
11
|
+
gem.homepage = "http://github.com/liangzan/define_columns"
|
12
|
+
gem.authors = ["Wong Liang Zan"]
|
13
|
+
gem.add_development_dependency "rspec", ">= 1.2.9"
|
14
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
15
|
+
end
|
16
|
+
Jeweler::GemcutterTasks.new
|
17
|
+
rescue LoadError
|
18
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
19
|
+
end
|
20
|
+
|
21
|
+
require 'spec/rake/spectask'
|
22
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
23
|
+
spec.libs << 'lib' << 'spec'
|
24
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
25
|
+
end
|
26
|
+
|
27
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
28
|
+
spec.libs << 'lib' << 'spec'
|
29
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
30
|
+
spec.rcov = true
|
31
|
+
end
|
32
|
+
|
33
|
+
task :spec => :check_dependencies
|
34
|
+
|
35
|
+
task :default => :spec
|
36
|
+
|
37
|
+
require 'rake/rdoctask'
|
38
|
+
Rake::RDocTask.new do |rdoc|
|
39
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
40
|
+
|
41
|
+
rdoc.rdoc_dir = 'rdoc'
|
42
|
+
rdoc.title = "define_columns #{version}"
|
43
|
+
rdoc.rdoc_files.include('README*')
|
44
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
45
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'define_columns/table_column'
|
2
|
+
require 'define_columns/table_columns'
|
3
|
+
|
4
|
+
module DefineColumns
|
5
|
+
|
6
|
+
def self.included(klass)
|
7
|
+
klass.class_eval do
|
8
|
+
extend DefineColumns::ClassMethods
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
module ClassMethods
|
13
|
+
|
14
|
+
def table_columns
|
15
|
+
@table_cols.columns
|
16
|
+
end
|
17
|
+
|
18
|
+
def define_columns
|
19
|
+
@table_cols = TableColumns.new(self.content_columns.map(&:name))
|
20
|
+
yield @table_cols
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
ActiveRecord::Base.class_eval { include DefineColumns }
|
@@ -0,0 +1,28 @@
|
|
1
|
+
class TableColumn
|
2
|
+
|
3
|
+
attr_accessor :name
|
4
|
+
|
5
|
+
def initialize(name)
|
6
|
+
@name = name
|
7
|
+
end
|
8
|
+
|
9
|
+
def header
|
10
|
+
@name.humanize
|
11
|
+
end
|
12
|
+
|
13
|
+
def show(subject)
|
14
|
+
if defined?(@proc)
|
15
|
+
@proc.call subject.send(@name.to_sym)
|
16
|
+
else
|
17
|
+
subject.send @name.to_sym
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
def apply(&blk)
|
23
|
+
@proc = blk
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
|
@@ -0,0 +1,23 @@
|
|
1
|
+
class TableColumns
|
2
|
+
|
3
|
+
attr_accessor :columns
|
4
|
+
|
5
|
+
def initialize(cols)
|
6
|
+
@columns = cols.map { |c| TableColumn.new(c) }
|
7
|
+
end
|
8
|
+
|
9
|
+
def add(cols)
|
10
|
+
new_cols = cols.map { |c| TableColumn.new(c) }
|
11
|
+
@columns = @columns + new_cols
|
12
|
+
end
|
13
|
+
|
14
|
+
def hide(cols)
|
15
|
+
@columns = @columns.reject { |col| cols.include?(col.name) }
|
16
|
+
end
|
17
|
+
|
18
|
+
def show(col, &blk)
|
19
|
+
index = @columns.map(&:name).index(col.to_s)
|
20
|
+
@columns[index].apply(&blk)
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe "DefineColumns" do
|
4
|
+
|
5
|
+
before(:each) do
|
6
|
+
@original_content_cols = [ mock_active_record("house"), mock_active_record("car"), mock_active_record("room"), mock_active_record("television") ]
|
7
|
+
MockModel.stubs(:content_columns).returns(@original_content_cols)
|
8
|
+
end
|
9
|
+
|
10
|
+
def mock_active_record(name)
|
11
|
+
mock_active_record = mock()
|
12
|
+
mock_active_record.stubs(:name).returns(name)
|
13
|
+
mock_active_record
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should hide the defined content columns" do
|
17
|
+
MockModel.class_eval do
|
18
|
+
define_columns do |columns|
|
19
|
+
columns.hide %w(house)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
MockModel.table_columns.map(&:name).include?("house").should == false
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should add the defined content columns" do
|
27
|
+
MockModel.class_eval do
|
28
|
+
define_columns do |columns|
|
29
|
+
columns.add %w(lamp)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
MockModel.table_columns.map(&:name).include?("lamp").should == true
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should customize the output of the defined content columns" do
|
37
|
+
MockModel.class_eval do
|
38
|
+
def road
|
39
|
+
%\Robinson Road\
|
40
|
+
end
|
41
|
+
|
42
|
+
define_columns do |columns|
|
43
|
+
columns.add %w(road)
|
44
|
+
columns.show(:road) do |road|
|
45
|
+
%\-#{road}-\
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
MockModel.table_columns.each do |col|
|
51
|
+
if col.name.eql?("road")
|
52
|
+
col.show(MockModel.new).should == %\-Robinson Road-\
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
it "should show the column name in a humanized manner" do
|
58
|
+
MockModel.class_eval do
|
59
|
+
def traffic_jam
|
60
|
+
%\Traffic Jam\
|
61
|
+
end
|
62
|
+
|
63
|
+
define_columns do |columns|
|
64
|
+
columns.add %w(traffic_jam)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
MockModel.table_columns.each do |col|
|
69
|
+
if col.name.eql?("traffic_jam")
|
70
|
+
col.header.should == %\Traffic jam\
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
data/spec/spec.opts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'spec'
|
2
|
+
require 'spec/autorun'
|
3
|
+
require 'rubygems'
|
4
|
+
require 'mocha'
|
5
|
+
require 'active_support'
|
6
|
+
require 'active_record'
|
7
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
8
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
9
|
+
require 'define_columns'
|
10
|
+
|
11
|
+
Spec::Runner.configure do |config|
|
12
|
+
|
13
|
+
end
|
14
|
+
|
15
|
+
class MockModel
|
16
|
+
include DefineColumns
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: define_columns
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Wong Liang Zan
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-01-30 00:00:00 +08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rspec
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 1.2.9
|
24
|
+
version:
|
25
|
+
description: define_columns allows you to add, hide, customize the output of your columns
|
26
|
+
email: zan@liangzan.net
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- LICENSE
|
33
|
+
- README.rdoc
|
34
|
+
files:
|
35
|
+
- .document
|
36
|
+
- .gitignore
|
37
|
+
- LICENSE
|
38
|
+
- README.rdoc
|
39
|
+
- Rakefile
|
40
|
+
- VERSION
|
41
|
+
- lib/define_columns.rb
|
42
|
+
- lib/define_columns/table_column.rb
|
43
|
+
- lib/define_columns/table_columns.rb
|
44
|
+
- spec/define_columns_spec.rb
|
45
|
+
- spec/spec.opts
|
46
|
+
- spec/spec_helper.rb
|
47
|
+
has_rdoc: true
|
48
|
+
homepage: http://github.com/liangzan/define_columns
|
49
|
+
licenses: []
|
50
|
+
|
51
|
+
post_install_message:
|
52
|
+
rdoc_options:
|
53
|
+
- --charset=UTF-8
|
54
|
+
require_paths:
|
55
|
+
- lib
|
56
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: "0"
|
61
|
+
version:
|
62
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: "0"
|
67
|
+
version:
|
68
|
+
requirements: []
|
69
|
+
|
70
|
+
rubyforge_project:
|
71
|
+
rubygems_version: 1.3.5
|
72
|
+
signing_key:
|
73
|
+
specification_version: 3
|
74
|
+
summary: Customizable content columns for Rails
|
75
|
+
test_files:
|
76
|
+
- spec/define_columns_spec.rb
|
77
|
+
- spec/spec_helper.rb
|