active-component 0.1.0 → 0.1.1

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: b475c4c94a10c124c9e7f1abde4e94457bd676ff
4
- data.tar.gz: 50687bf5b33b5d32d9d940014ff8410cf004ebcf
3
+ metadata.gz: a25b946f3c2dcac2bc08f225ee0750fc92b8dd3b
4
+ data.tar.gz: 4bd337f140175fa144da9646015be48c5bb5981a
5
5
  SHA512:
6
- metadata.gz: 6d0c880b34ddcc2c3c400b77da985d125fc1347f7167408cd2a420eee984df6ebbdebad3c16bbeeb265d8f9139874bb36d2583734bd960e65f96c54cf272d530
7
- data.tar.gz: fc09071da743121546495a67b5e638e452b78191cb60e41f4330f7950d1652a760f66bf9299707e1f6e56da34d435b32b13d774755bd22c16909cbc7c607a136
6
+ metadata.gz: 05d28e9c891720503d97aa596afb2bd37716e4e79f7d8b0368cec63e6d42b218dd9ad8f1cda7e5c1feab50cdd0d36ea390f4cfd4d727f3ab719fa49108d780e8
7
+ data.tar.gz: 0c60c0052b1ce195d4165272b899f7722d4fb42bae956905d8eb37f76a61d5da1df1d9bd6747a2588a01e7b977f758087fd31cfc3bb197fc98bc73f4fcc42120
@@ -35,17 +35,17 @@ module ActiveComponent
35
35
  # self.controller from anywhere in the inherited classes.
36
36
  #
37
37
  def initialize(args = {})
38
- if args.kind_of? Hash
39
- assign_variables args
40
- @controller = ActiveComponent.get_controller rescue nil
41
- else
42
- raise ArgumentError, "Expected: Hash. Received: #{args.class.name}"
43
- end
38
+ assign_variables args
39
+ @controller = ActiveComponent.get_controller
44
40
  end
45
41
 
46
42
  def assign_variables(args)
47
- args.each do |key, value|
48
- self.send("#{key}=", value)
43
+ if args.kind_of? Hash
44
+ args.each do |key, value|
45
+ self.send("#{key}=", value)
46
+ end
47
+ else
48
+ raise ArgumentError, "Expected: Hash. Received: #{args.class.name}"
49
49
  end
50
50
  end
51
51
 
@@ -23,7 +23,7 @@ module ActiveComponent
23
23
  unless args[:collection].present?
24
24
  template(type, args)
25
25
  else
26
- collection(type, args[:collection], args[:as])
26
+ collection(type, args[:collection], args[:as], args[:locals])
27
27
  end
28
28
  end
29
29
 
@@ -34,13 +34,21 @@ module ActiveComponent
34
34
  resource.render
35
35
  end
36
36
 
37
- def collection(type, collection, as)
37
+ def collection(type, collection, as, locals)
38
+
38
39
  error_string = "Attempting to render a collection without specifying the ':as' value"
39
40
  raise ArgumentError, error_string if as.nil?
40
41
 
41
- collection.map do |item|
42
- template(type, as => item)
43
- end.join('')
42
+ locals = locals || {}
43
+
44
+ html_string = collection.map do |item|
45
+ template(
46
+ type,
47
+ locals.merge(as => item)
48
+ )
49
+ end
50
+
51
+ html_string.join('')
44
52
  end
45
53
 
46
54
  def constantize(type)
@@ -1,3 +1,3 @@
1
1
  module ActiveComponent
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -33,4 +33,29 @@ class ActiveComponent::BaseTest < ActiveSupport::TestCase
33
33
  @foo.render_component(:foo, :bar => "bar")
34
34
  end
35
35
  end
36
+
37
+ #
38
+ # Unit tests
39
+ #
40
+
41
+ test "only accepts hashes" do
42
+ assert_raise ArgumentError do
43
+ @foo.assign_variables('a')
44
+ end
45
+ end
46
+
47
+ test "assigns from the given hash to the instance variables" do
48
+ @foo.assign_variables(:bar => "foo")
49
+ assert_equal @foo.bar, "foo"
50
+ end
51
+
52
+ test "has a controller method" do
53
+ FooComponent.any_instance.stubs(:controller).returns(true)
54
+ assert @foo.controller
55
+ end
56
+
57
+ test "has a render method" do
58
+ assert @foo.render
59
+ end
60
+
36
61
  end
@@ -21,4 +21,14 @@ class ActiveComponent::ContextTest < ActiveSupport::TestCase
21
21
  controller.get_context
22
22
  assert_equal controller, ActiveComponent.controllers[Thread.current.object_id]
23
23
  end
24
+
25
+ test "includes the before filter into action controller" do
26
+ assert_equal :get_context, ActionController::Base.new._process_action_callbacks.first.filter
27
+ end
28
+
29
+ test "gets the context for a given contrlller" do
30
+ assert_nothing_raised Exception do
31
+ ActionController::Base.new.get_context
32
+ end
33
+ end
24
34
  end
@@ -1,15 +1,4 @@
1
1
  require 'test_helper'
2
2
 
3
3
  class ActiveComponent::RailtieTest < ActiveSupport::TestCase
4
-
5
- test "has before filter" do
6
- assert_equal :get_context, ActionController::Base.new._process_action_callbacks.first.filter
7
- end
8
-
9
- test "has the context method" do
10
- assert_nothing_raised Exception do
11
- ActionController::Base.new.get_context
12
- end
13
- end
14
-
15
4
  end
@@ -3,8 +3,8 @@ require 'test_helper'
3
3
  class ActiveComponent::RenderableTest < ActiveSupport::TestCase
4
4
 
5
5
  setup do
6
- FooComponent.any_instance.stubs(:render).returns("<div>hello!</div>")
7
6
  @view = ActionView::Base.new
7
+ @collection = ["hello!", "world!"]
8
8
  end
9
9
 
10
10
  test "renders a component" do
@@ -24,8 +24,8 @@ class ActiveComponent::RenderableTest < ActiveSupport::TestCase
24
24
  end
25
25
 
26
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
27
+ string = @view.render_component(:foo, :collection => @collection, :as => :bar)
28
+ assert_equal "<div>hello!</div><div>world!</div>", string
29
29
  end
30
30
 
31
31
  test "it raises an error if the given arguments didn't include the :as argument" do
@@ -34,4 +34,38 @@ class ActiveComponent::RenderableTest < ActiveSupport::TestCase
34
34
  end
35
35
  end
36
36
 
37
+ test "it accepts locals for a given collection" do
38
+ string = @view.render_component(
39
+ :foo,
40
+ :collection => @collection,
41
+ :as => :bar,
42
+ :locals => { :baz => "baz" }
43
+ )
44
+ assert_match /baz/, string
45
+ end
46
+
47
+ #
48
+ # Private methods
49
+ #
50
+
51
+ test "it returns a string of multiple elements" do
52
+ string = @view.send(:collection, :foo, @collection, :bar, nil)
53
+ assert_equal "<div>hello!</div><div>world!</div>", string
54
+ end
55
+
56
+ test "it returns a string from the template" do
57
+ string = @view.send(:template, :foo, :bar => "Hello world!")
58
+ assert_equal "<div>Hello world!</div>", string
59
+ end
60
+
61
+ test "it turns a string into a constant" do
62
+ assert_equal FooComponent, @view.send(:constantize, "foo_component")
63
+ end
64
+
65
+ test "it raises an error" do
66
+ assert_raise ArgumentError do
67
+ @view.send(:raise_errors, :foo, {})
68
+ end
69
+ end
70
+
37
71
  end
@@ -1,3 +1,7 @@
1
1
  class FooComponent < ActiveComponent::Base
2
- attr_accessor :bar
2
+ attr_accessor :bar, :baz
3
+
4
+ def render
5
+ "<div>#{bar}</div>#{baz}"
6
+ end
3
7
  end
data/test/test_helper.rb CHANGED
@@ -6,6 +6,9 @@ require "rails/test_help"
6
6
  require 'faker'
7
7
  require 'pry'
8
8
  require 'mocha/setup'
9
+ require 'coveralls'
10
+
11
+ Coveralls.wear!
9
12
 
10
13
  Rails.backtrace_cleaner.remove_silencers!
11
14
 
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.1.0
4
+ version: 0.1.1
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-08 00:00:00.000000000 Z
12
+ date: 2013-10-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -95,6 +95,20 @@ dependencies:
95
95
  - - '>='
96
96
  - !ruby/object:Gem::Version
97
97
  version: '0'
98
+ - !ruby/object:Gem::Dependency
99
+ name: coveralls
100
+ requirement: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - '>='
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ type: :development
106
+ prerelease: false
107
+ version_requirements: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - '>='
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
98
112
  description: A View-Component framework for Rails applications.
99
113
  email:
100
114
  - cavjzz@gmail.com