genki-merb_component 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
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
@@ -5,7 +5,7 @@ require 'merb-core'
5
5
  require 'merb-core/tasks/merb'
6
6
 
7
7
  GEM_NAME = "merb_component"
8
- GEM_VERSION = "0.0.3"
8
+ GEM_VERSION = "0.1.0"
9
9
  AUTHOR = "Genki Takiuchi"
10
10
  EMAIL = "genki@s21g.com"
11
11
  HOMEPAGE = "http://blog.s21g.com/genki"
@@ -9,6 +9,7 @@ if defined?(Merb::Plugins)
9
9
  Merb::BootLoader.before_app_loads do
10
10
  # require code that must be loaded before the application
11
11
  require 'merb_component/controller_ext'
12
+ require 'merb_component/model_ext'
12
13
  end
13
14
 
14
15
  Merb::BootLoader.after_app_loads do
@@ -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.3
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