active-component 0.0.9 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b304b0dceca835c9b9a814cc5feec1cafd10e8e3
4
- data.tar.gz: c3f4b3012ba80eb84e7daed051c41288477a378e
3
+ metadata.gz: b475c4c94a10c124c9e7f1abde4e94457bd676ff
4
+ data.tar.gz: 50687bf5b33b5d32d9d940014ff8410cf004ebcf
5
5
  SHA512:
6
- metadata.gz: 3892ae5c26b96f6443ecf6b43a86c1e7b8a8249cdf13e41df3a99ca67f28d18979d137edbb647059e5f58c6bbe46d362f922162f02de072dec25934eee6efef3
7
- data.tar.gz: 2f939e9b04fef059d45103f5ac84f6c7269c9b92166907a73f50d3617620dff957a4349ef2780ebb246bab6dada34d6dd667f07c91a79bf68c8dee7966647c97
6
+ metadata.gz: 6d0c880b34ddcc2c3c400b77da985d125fc1347f7167408cd2a420eee984df6ebbdebad3c16bbeeb265d8f9139874bb36d2583734bd960e65f96c54cf272d530
7
+ data.tar.gz: fc09071da743121546495a67b5e638e452b78191cb60e41f4330f7950d1652a760f66bf9299707e1f6e56da34d435b32b13d774755bd22c16909cbc7c607a136
@@ -1,7 +1,7 @@
1
- require 'active_component/base'
2
1
  require 'active_component/renderable'
3
2
  require 'active_component/context'
4
3
  require 'active_component/railtie' if defined?(Rails)
4
+ require 'active_component/base'
5
5
 
6
6
  module ActiveComponent
7
7
  end
@@ -1,4 +1,5 @@
1
1
  require 'mustache'
2
+
2
3
  module ActiveComponent
3
4
  #
4
5
  # We inherit from mustache to have a default template engine
@@ -12,6 +13,7 @@ module ActiveComponent
12
13
  # the rails conventions, especially with the asset pipeline.
13
14
  #
14
15
  include ActionView::Helpers::DateHelper
16
+ include ActiveComponent::Renderable
15
17
  #
16
18
  # Whenever the user creates a new object from a class inherited
17
19
  # from ActiveComponent::Base it needs to define its attributes as
@@ -6,19 +6,53 @@ require 'active_support/concern'
6
6
  #
7
7
  # <%= render_component(:view, var: var) %>
8
8
  #
9
+ # In addition to that you can render a collection of components using
10
+ # the same helper method
11
+ #
12
+ # <%= render_component(:view, collection: views, as: :view)%>
13
+ #
14
+ # The :as argument helps you to provide the instance variable name that
15
+ # you defined int your component class and it will be received like that.
16
+ #
9
17
  module ActiveComponent
10
18
  module Renderable
11
19
 
12
20
  def render_component(type, args={})
21
+ raise_errors(type, args) if type.nil? or args.nil?
22
+
23
+ unless args[:collection].present?
24
+ template(type, args)
25
+ else
26
+ collection(type, args[:collection], args[:as])
27
+ end
28
+ end
29
+
30
+ private
31
+
32
+ def template(type, args)
13
33
  resource = constantize("#{type.to_s}_component").new(args)
14
34
  resource.render
15
35
  end
16
36
 
37
+ def collection(type, collection, as)
38
+ error_string = "Attempting to render a collection without specifying the ':as' value"
39
+ raise ArgumentError, error_string if as.nil?
40
+
41
+ collection.map do |item|
42
+ template(type, as => item)
43
+ end.join('')
44
+ end
45
+
17
46
  def constantize(type)
18
47
  type = type.camelize.constantize
19
48
  type
20
49
  end
21
50
 
51
+ def raise_errors(type, args)
52
+ text = "Invalid arguments. Received: #{type.class}, #{args.class}. Expected: Symbol, Hash"
53
+ raise ArgumentError, text
54
+ end
55
+
22
56
  end
23
57
  end
24
58
 
@@ -1,3 +1,3 @@
1
1
  module ActiveComponent
2
- VERSION = "0.0.9"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -27,4 +27,10 @@ class ActiveComponent::BaseTest < ActiveSupport::TestCase
27
27
  assert_kind_of FooController, @foo.controller
28
28
  end
29
29
 
30
+ test "has the action view helpers available" do
31
+ assert_nothing_raised Exception do
32
+ FooComponent.any_instance.stubs(:render).returns("")
33
+ @foo.render_component(:foo, :bar => "bar")
34
+ end
35
+ end
30
36
  end
@@ -2,16 +2,36 @@ require 'test_helper'
2
2
 
3
3
  class ActiveComponent::RenderableTest < ActiveSupport::TestCase
4
4
 
5
+ setup do
6
+ FooComponent.any_instance.stubs(:render).returns("<div>hello!</div>")
7
+ @view = ActionView::Base.new
8
+ end
9
+
5
10
  test "renders a component" do
6
- FooComponent.any_instance.stubs(:render).returns("")
7
- assert_nothing_raised TypeError do
8
- ActionView::Base.new.render_component(:foo)
11
+ assert_nothing_raised Exception do
12
+ @view.render_component(:foo)
9
13
  end
10
14
  end
11
15
 
12
16
  test "gets a component instance from symbol" do
13
- FooComponent.any_instance.stubs(:render).returns("")
14
- assert_kind_of String, ActionView::Base.new.render_component(:foo)
17
+ assert_kind_of String, @view.render_component(:foo)
18
+ end
19
+
20
+ test "accepts only a symbol and a hash" do
21
+ assert_raise ArgumentError do
22
+ @view.render_component(nil, nil)
23
+ end
24
+ end
25
+
26
+ test "it renders multiple components if receives a collection array" do
27
+ string = @view.render_component(:foo, :collection => [1,2], :as => :bar)
28
+ assert_equal "<div>hello!</div><div>hello!</div>", string
29
+ end
30
+
31
+ test "it raises an error if the given arguments didn't include the :as argument" do
32
+ assert_raise ArgumentError do
33
+ @view.render_component(:foo, :collection => [1,2])
34
+ end
15
35
  end
16
36
 
17
37
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active-component
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Antonio Chavez
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-10-07 00:00:00.000000000 Z
12
+ date: 2013-10-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -122,7 +122,6 @@ files:
122
122
  - test/active_component_test.rb
123
123
  - test/dummy/application.rb
124
124
  - test/generators/component_generator_test.rb
125
- - test/support/foo_component.mustache
126
125
  - test/support/foo_component.rb
127
126
  - test/support/foo_controller.rb
128
127
  - test/test_helper.rb
@@ -158,7 +157,6 @@ test_files:
158
157
  - test/active_component_test.rb
159
158
  - test/dummy/application.rb
160
159
  - test/generators/component_generator_test.rb
161
- - test/support/foo_component.mustache
162
160
  - test/support/foo_component.rb
163
161
  - test/support/foo_controller.rb
164
162
  - test/test_helper.rb
@@ -1 +0,0 @@
1
-