listable 0.1.4 → 0.1.5
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.rdoc +72 -2
- data/Rakefile +1 -1
- data/VERSION +1 -1
- data/listable.gemspec +2 -2
- metadata +4 -4
data/README.rdoc
CHANGED
@@ -2,10 +2,80 @@
|
|
2
2
|
|
3
3
|
With listable you can consolidate fields from several models into one, backed up by a database view.
|
4
4
|
It is perfect for e.g. a front page where you may want to display the most recent additions to your site in a joint list.
|
5
|
-
By providing scopes for each model you wish to include in a view,
|
5
|
+
By providing scopes for each model you wish to include in a view, listable will automatically create the database view for you.
|
6
|
+
The view model and the models it lists will have a polymorphic association for convenience.
|
7
|
+
Listable currently supports the following connection adapters: SQLite, PostgreSQL and MySQL2.
|
6
8
|
|
7
9
|
== How to use
|
8
|
-
|
10
|
+
This chapter shows some basic examples of all the steps needed to get started.
|
11
|
+
|
12
|
+
=== Configuring the models
|
13
|
+
Every model that should be listable in the same context must provide a scope that select columns of the same name,
|
14
|
+
in the same order and with compatible data types. Listable extends ActiveRecord with two query methods to make this easy
|
15
|
+
without being forced to write database specific SQL in your models.
|
16
|
+
N.B. The timestamps "created_at" and "updated_at" will be include automatically, so don't include them in the scope.
|
17
|
+
|
18
|
+
Here is a basic example:
|
19
|
+
class Employee << ActiveRecord::Base
|
20
|
+
# This model will be listable through the view backed model ListItem,
|
21
|
+
# with the data provided by the "listables" scope
|
22
|
+
listable_through :list_item, :listables
|
23
|
+
|
24
|
+
# The "first_name" and "last_name" columns will be concatenated and selected as "name"
|
25
|
+
scope :listables, concat_select([:first_name, ' ', :last_name], :name)
|
26
|
+
end
|
27
|
+
|
28
|
+
class Article << ActiveRecord::Base
|
29
|
+
# You can also provide the target model name in plural if that feels more natural for you
|
30
|
+
listable_through :list_items, :listables
|
31
|
+
|
32
|
+
# The select_as method takes a hash as argument, the keys are the original column names
|
33
|
+
# and the values are their new names
|
34
|
+
scope :listables, select_as(title: 'name')
|
35
|
+
end
|
36
|
+
|
37
|
+
class ListItem << ActiveRecord::Base
|
38
|
+
# This method call is needed to configure the model correctly
|
39
|
+
acts_as_listable_view
|
40
|
+
end
|
41
|
+
|
42
|
+
=== Creating the database views
|
43
|
+
You can create the database views by running the rake task <em>listable:migrate</em>.
|
44
|
+
But you will hardly ever need to do that since all listable views are automatically (re)created when running
|
45
|
+
Rails' <em>db:migrate</em>.
|
46
|
+
|
47
|
+
==== Why not generate Rails migrations to create the views?
|
48
|
+
I believe it is very awkward to create views through migrations for several reasons.
|
49
|
+
The basic role of a migration is to either add or remove columns where you can store data.
|
50
|
+
When creating a view, you are not adding more means to store data. You are just creating an alternative
|
51
|
+
view of what's already there.
|
52
|
+
|
53
|
+
Since a view is 100% dependant on your existing columns, you will need
|
54
|
+
to recreate the view everytime you need to change the columns it depend on. Because that's the thing with
|
55
|
+
views, everytime you need to change it, you will have to drop it and create it again from scratch.
|
56
|
+
You can imagine how ugly, and not so very DRY, your migrations could end up.
|
57
|
+
|
58
|
+
There is also not any built in support for creating views in Rails, which means you would probably end
|
59
|
+
up writing database specific SQL, which could potentially become a problem along the way.
|
60
|
+
|
61
|
+
Migrations is just not a natural place for views. Just let listable handle it for you instead...
|
62
|
+
|
63
|
+
=== Using your listable models
|
64
|
+
You can query your view backed model like any other model:
|
65
|
+
class ListItemsController < ApplicationController
|
66
|
+
def index
|
67
|
+
@items = ListItem.order("name")
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
The original model is accessible through the "listable" property.
|
72
|
+
|
73
|
+
# index.html.erb
|
74
|
+
render @items
|
75
|
+
|
76
|
+
# _list_item.html.erb
|
77
|
+
content_tag :h1, list_item.name
|
78
|
+
link_to list_item.listable # Links to the original model
|
9
79
|
|
10
80
|
== Contributing to listable
|
11
81
|
|
data/Rakefile
CHANGED
@@ -17,7 +17,7 @@ Jeweler::Tasks.new do |gem|
|
|
17
17
|
gem.name = "listable"
|
18
18
|
gem.homepage = "http://github.com/baldursson/listable"
|
19
19
|
gem.license = "MIT"
|
20
|
-
gem.summary = %Q{
|
20
|
+
gem.summary = %Q{ActiveRecord extension that makes it easy to list and query several models through a single view backed model.}
|
21
21
|
gem.description = %Q{With listable you can consolidate fields from several models into one, backed up by a database view.
|
22
22
|
It is perfect for e.g. a front page where you may want to display the most recent additions to your site in a joint list.
|
23
23
|
By providing scopes for each model you wish to include in a view, Listable will automatically create the database view for you.}
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.5
|
data/listable.gemspec
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "listable"
|
8
|
-
s.version = "0.1.
|
8
|
+
s.version = "0.1.5"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Johannes Baldursson"]
|
@@ -41,7 +41,7 @@ Gem::Specification.new do |s|
|
|
41
41
|
s.licenses = ["MIT"]
|
42
42
|
s.require_paths = ["lib"]
|
43
43
|
s.rubygems_version = "1.8.19"
|
44
|
-
s.summary = "
|
44
|
+
s.summary = "ActiveRecord extension that makes it easy to list and query several models through a single view backed model."
|
45
45
|
|
46
46
|
if s.respond_to? :specification_version then
|
47
47
|
s.specification_version = 3
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: listable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -234,7 +234,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
234
234
|
version: '0'
|
235
235
|
segments:
|
236
236
|
- 0
|
237
|
-
hash:
|
237
|
+
hash: -4561906636969881367
|
238
238
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
239
239
|
none: false
|
240
240
|
requirements:
|
@@ -246,6 +246,6 @@ rubyforge_project:
|
|
246
246
|
rubygems_version: 1.8.19
|
247
247
|
signing_key:
|
248
248
|
specification_version: 3
|
249
|
-
summary:
|
250
|
-
a single view backed model.
|
249
|
+
summary: ActiveRecord extension that makes it easy to list and query several models
|
250
|
+
through a single view backed model.
|
251
251
|
test_files: []
|