genki-merb_component 0.0.3 → 0.1.0
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 +14 -0
- data/Rakefile +1 -1
- data/lib/merb_component.rb +1 -0
- data/lib/merb_component/model_ext.rb +26 -0
- metadata +2 -1
data/README
CHANGED
@@ -2,3 +2,17 @@ merb_component
|
|
2
2
|
=============
|
3
3
|
|
4
4
|
Merb plugin that provides composition of controllers.
|
5
|
+
|
6
|
+
Example of use:
|
7
|
+
|
8
|
+
Content of the user (id is 2) goes here
|
9
|
+
<%= component User, :show, :id => 2 %>
|
10
|
+
|
11
|
+
Index of posts related with the @user go here
|
12
|
+
<% Post.related_with @user do %>
|
13
|
+
<%= component Post, :index %>
|
14
|
+
<% end %>
|
15
|
+
|
16
|
+
In app/views/posts/index.html.erb,
|
17
|
+
You can access to the corresponding relation like this
|
18
|
+
<%= Post.relation #=> @user %>
|
data/Rakefile
CHANGED
data/lib/merb_component.rb
CHANGED
@@ -0,0 +1,26 @@
|
|
1
|
+
module DataMapper::Model
|
2
|
+
def related_with(model, &block)
|
3
|
+
model_class = model.class
|
4
|
+
storage_name = model_class.storage_name
|
5
|
+
assoc_name = storage_name.singular.intern
|
6
|
+
key_names = relationships[assoc_name].child_key.map{|i| i.name}
|
7
|
+
push_relation(model)
|
8
|
+
with_scope(Hash[key_names.zip(model.key)], &block)
|
9
|
+
ensure
|
10
|
+
pop_relation
|
11
|
+
end
|
12
|
+
|
13
|
+
def relation
|
14
|
+
Thread::current[:relation].last
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
def push_relation(relation)
|
19
|
+
Thread::current[:relation] ||= []
|
20
|
+
Thread::current[:relation].push relation
|
21
|
+
end
|
22
|
+
|
23
|
+
def pop_relation
|
24
|
+
Thread::current[:relation].pop
|
25
|
+
end
|
26
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: genki-merb_component
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Genki Takiuchi
|
@@ -39,6 +39,7 @@ files:
|
|
39
39
|
- lib/merb_component
|
40
40
|
- lib/merb_component/controller_ext.rb
|
41
41
|
- lib/merb_component/merbtasks.rb
|
42
|
+
- lib/merb_component/model_ext.rb
|
42
43
|
- lib/merb_component.rb
|
43
44
|
- spec/merb_component_spec.rb
|
44
45
|
- spec/spec_helper.rb
|