active-component 0.0.5 → 0.0.6

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 250d060c002ceec3a69685cf0af4fa2088cdce7c
4
- data.tar.gz: 102f562d032f9c6dba3ce9a6f35768bc359eed61
3
+ metadata.gz: a7de4ea36b77ba8216a9ac0c9788cf5fdc15a64f
4
+ data.tar.gz: 5eba3cd8f644f1fdd92d2875c6176b895b162a8b
5
5
  SHA512:
6
- metadata.gz: f730cfc6de3d9dadad424771f751ecc90fd613cdb02c85806772459296881c41025998f04ba4fb7527cc721df1594281316ec7c8618b24f82076742e83ca527d
7
- data.tar.gz: bd5b759e2ed8b786386e9d1d8e2eaf6820a71b5b96c2c702eff08c0ea058979d455491888adaa0f2367936cbc882e6271892d0db89a036a225bf6024e37a1953
6
+ metadata.gz: 4619068c02a72ad5aeee95e4a6c9495fa551b41b5cd19515b798f104f34d64f6c96bbd1926e2fc0683048fde8c4910e29df26557ff4606096f4e01a4fe0b9cab
7
+ data.tar.gz: 24f96ae408f07f70d7464dd26105a90c300c1852914e47da7226930694f2ae96db0fb005b5c78f0093fb5cd41453dc528b7bb70f629286fc01026700ab3ab71e
@@ -51,5 +51,9 @@ module ActiveComponent
51
51
  def controller
52
52
  @controller
53
53
  end
54
+
55
+ def render
56
+ super.html_safe
57
+ end
54
58
  end
55
59
  end
@@ -1,4 +1,5 @@
1
1
  #
2
+ #
2
3
  # This is an autoincluded module for ActionController::Base
3
4
  #
4
5
  # It will store the controller context for the given request in a
@@ -15,6 +16,15 @@ module ActiveComponent
15
16
  @@controllers = {}
16
17
 
17
18
  class << self
19
+
20
+ def controllers
21
+ if Rails.env.test?
22
+ @@controllers
23
+ else
24
+ puts "WARNING: ActiveComponent#controllers cannot be accessible from outside of the class"
25
+ end
26
+ end
27
+
18
28
  def get_controller
19
29
  @@controllers[Thread.current.object_id]
20
30
  end
@@ -6,7 +6,6 @@ module ActiveComponent
6
6
  initializer "Appends a method to the action controller class" do
7
7
  ActiveSupport.on_load(:action_controller) do
8
8
  ActionController::Base.send(:include, ActiveComponent::Context)
9
- ActionController::Base.send(:include, ActiveComponent::Renderable)
10
9
  end
11
10
  end
12
11
  end
@@ -1,20 +1,28 @@
1
1
  require 'active_support/concern'
2
2
 
3
3
  #
4
- # Work In Progress
4
+ # This provides a clean implementation for rendering your template
5
+ # inside your view.
5
6
  #
6
- # The idea is to provide a clean rails-like methods to
7
- # render view components nicely from the view or even respond
8
- # with them from a controller instance
9
- #
10
- # render_component(:view_component, var: var)
7
+ # <%= render_component(:view, var: var) %>
11
8
  #
12
9
  module ActiveComponent
13
10
  module Renderable
14
- extend ActiveSupport::Concern
15
11
 
16
- def render_component
17
- ""
12
+ def render_component(type, args={})
13
+ resource = constantize("#{type.to_s}_component").new(args)
14
+ resource.render
15
+ end
16
+
17
+ def constantize(type)
18
+ type = type.camelize.constantize
19
+ unless type.superclass == ActiveComponent::Base
20
+ raise TypeError, ":type must be compliant to any existing component class"
21
+ end
22
+ type
18
23
  end
24
+
19
25
  end
20
26
  end
27
+
28
+ ActionView::Base.send(:include, ActiveComponent::Renderable)
@@ -1,3 +1,3 @@
1
1
  module ActiveComponent
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -4,7 +4,7 @@ class ActiveComponent::BaseTest < ActiveSupport::TestCase
4
4
 
5
5
  setup do
6
6
  FooController.new.get_context
7
- @foo = Foo.new(bar: "bar")
7
+ @foo = FooComponent.new(bar: "bar")
8
8
  end
9
9
 
10
10
  test "has included date helper from action view" do
@@ -12,11 +12,11 @@ class ActiveComponent::BaseTest < ActiveSupport::TestCase
12
12
  end
13
13
 
14
14
  test "a new instance of active_component/base accepts only a hash of arguments" do
15
- assert_nothing_raised(ArgumentError, nil) { Foo.new(bar: Faker::Name.name) }
15
+ assert_nothing_raised(ArgumentError, nil) { FooComponent.new(bar: Faker::Name.name) }
16
16
  end
17
17
 
18
18
  test "a new instance of active component should raise an error if the given value is not a hash" do
19
- assert_raise(ArgumentError, nil) { Foo.new(Faker::Lorem.sentence) }
19
+ assert_raise(ArgumentError, nil) { FooComponent.new(Faker::Lorem.sentence) }
20
20
  end
21
21
 
22
22
  test "assigns the values to instance variables" do
@@ -2,9 +2,24 @@ require 'test_helper'
2
2
 
3
3
  class ActiveComponent::ContextTest < ActiveSupport::TestCase
4
4
 
5
- test "returns a cotroller instance" do
5
+ def setup
6
6
  FooController.new.get_context
7
+ end
8
+
9
+ test "returns a cotroller instance" do
7
10
  assert_kind_of FooController, ActiveComponent.get_controller
8
11
  end
9
12
 
13
+ test "saves the controller in a @@controllers hash" do
14
+ assert_nothing_raised Exception do
15
+ ActiveComponent.set_controller(FooController.new)
16
+ end
17
+ end
18
+
19
+ test "it returns exactly the same controller sent" do
20
+ controller = FooController.new
21
+ controller.get_context
22
+ assert_equal controller, ActiveComponent.controllers[Thread.current.object_id]
23
+ end
24
+
10
25
  end
@@ -2,8 +2,14 @@ require 'test_helper'
2
2
 
3
3
  class ActiveComponent::RailtieTest < ActiveSupport::TestCase
4
4
 
5
- test "get the controller context" do
6
- assert FooController.new.render_component
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
7
13
  end
8
14
 
9
15
  end
@@ -3,7 +3,15 @@ require 'test_helper'
3
3
  class ActiveComponent::RenderableTest < ActiveSupport::TestCase
4
4
 
5
5
  test "renders a component" do
6
- assert_kind_of(String, FooController.new.render_component)
6
+ FooComponent.any_instance.stubs(:render).returns("")
7
+ assert_nothing_raised TypeError do
8
+ ActionView::Base.new.render_component(:foo)
9
+ end
10
+ end
11
+
12
+ 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)
7
15
  end
8
16
 
9
17
  end
@@ -2777,3 +2777,68 @@ ActiveComponent::RenderableTest: test_renders_a_component
2777
2777
  ActiveComponentTest: test_has_a_get_controller_method
2778
2778
  -----------------------------------------------------
2779
2779
   (0.1ms) rollback transaction
2780
+  (0.3ms) begin transaction
2781
+ --------------------------------------------------------------------------------------------------------
2782
+ ActiveComponent::BaseTest: test_a_new_instance_of_active_component/base_accepts_only_a_hash_of_arguments
2783
+ --------------------------------------------------------------------------------------------------------
2784
+  (0.1ms) rollback transaction
2785
+  (0.1ms) begin transaction
2786
+ -------------------------------------------------------------------------------------------------------------------------
2787
+ ActiveComponent::BaseTest: test_a_new_instance_of_active_component_should_raise_an_error_if_the_given_value_is_not_a_hash
2788
+ -------------------------------------------------------------------------------------------------------------------------
2789
+  (0.0ms) rollback transaction
2790
+  (0.1ms) begin transaction
2791
+ ------------------------------------------------------------------------
2792
+ ActiveComponent::BaseTest: test_assigns_the_values_to_instance_variables
2793
+ ------------------------------------------------------------------------
2794
+  (0.0ms) rollback transaction
2795
+  (0.0ms) begin transaction
2796
+ -------------------------------------------------------------------------
2797
+ ActiveComponent::BaseTest: test_has_included_date_helper_from_action_view
2798
+ -------------------------------------------------------------------------
2799
+  (0.1ms) rollback transaction
2800
+  (0.0ms) begin transaction
2801
+ -------------------------------------------------------------------------
2802
+ ActiveComponent::BaseTest: test_sets_controller_for_the_component_context
2803
+ -------------------------------------------------------------------------
2804
+  (0.1ms) rollback transaction
2805
+  (0.1ms) begin transaction
2806
+ ------------------------------------------------------------------------------
2807
+ ActiveComponent::ContextTest: test_it_returns_exactly_the_same_controller_sent
2808
+ ------------------------------------------------------------------------------
2809
+  (0.1ms) rollback transaction
2810
+  (0.1ms) begin transaction
2811
+ ---------------------------------------------------------------
2812
+ ActiveComponent::ContextTest: test_returns_a_cotroller_instance
2813
+ ---------------------------------------------------------------
2814
+  (0.1ms) rollback transaction
2815
+  (0.1ms) begin transaction
2816
+ -------------------------------------------------------------------------------
2817
+ ActiveComponent::ContextTest: test_saves_the_controller_in_a_@@controllers_hash
2818
+ -------------------------------------------------------------------------------
2819
+  (0.0ms) rollback transaction
2820
+  (0.0ms) begin transaction
2821
+ ----------------------------------------------------
2822
+ ActiveComponent::RailtieTest: test_has_before_filter
2823
+ ----------------------------------------------------
2824
+  (0.1ms) rollback transaction
2825
+  (0.0ms) begin transaction
2826
+ ---------------------------------------------------------
2827
+ ActiveComponent::RailtieTest: test_has_the_context_method
2828
+ ---------------------------------------------------------
2829
+  (0.0ms) rollback transaction
2830
+  (0.0ms) begin transaction
2831
+ ---------------------------------------------------------------------------
2832
+ ActiveComponent::RenderableTest: test_gets_a_component_instance_from_symbol
2833
+ ---------------------------------------------------------------------------
2834
+  (0.1ms) rollback transaction
2835
+  (0.0ms) begin transaction
2836
+ ---------------------------------------------------------
2837
+ ActiveComponent::RenderableTest: test_renders_a_component
2838
+ ---------------------------------------------------------
2839
+  (0.1ms) rollback transaction
2840
+  (0.0ms) begin transaction
2841
+ -----------------------------------------------------
2842
+ ActiveComponentTest: test_has_a_get_controller_method
2843
+ -----------------------------------------------------
2844
+  (0.0ms) rollback transaction
@@ -0,0 +1 @@
1
+
@@ -0,0 +1,3 @@
1
+ class FooComponent < ActiveComponent::Base
2
+ attr_accessor :bar
3
+ end
data/test/test_helper.rb CHANGED
@@ -5,6 +5,7 @@ require File.expand_path("../dummy/config/environment.rb", __FILE__)
5
5
  require "rails/test_help"
6
6
  require 'faker'
7
7
  require 'pry'
8
+ require 'mocha/setup'
8
9
 
9
10
  Rails.backtrace_cleaner.remove_silencers!
10
11
 
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.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Antonio Chavez
@@ -81,6 +81,20 @@ dependencies:
81
81
  - - '>='
82
82
  - !ruby/object:Gem::Version
83
83
  version: '0'
84
+ - !ruby/object:Gem::Dependency
85
+ name: mocha
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - '>='
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
84
98
  description: A View-Component framework for Rails applications.
85
99
  email:
86
100
  - cavjzz@gmail.com
@@ -159,7 +173,8 @@ files:
159
173
  - test/dummy/tmp/cache/assets/development/sprockets/edbef6e0d0a4742346cf479f2c522eb0
160
174
  - test/dummy/tmp/cache/assets/development/sprockets/f7cbd26ba1d28d48de824f0e94586655
161
175
  - test/generators/component_generator_test.rb
162
- - test/support/foo.rb
176
+ - test/support/foo_component.mustache
177
+ - test/support/foo_component.rb
163
178
  - test/support/foo_controller.rb
164
179
  - test/test_helper.rb
165
180
  homepage: https://github.com/TheNaoX/active_component
@@ -245,7 +260,8 @@ test_files:
245
260
  - test/dummy/tmp/cache/assets/development/sprockets/edbef6e0d0a4742346cf479f2c522eb0
246
261
  - test/dummy/tmp/cache/assets/development/sprockets/f7cbd26ba1d28d48de824f0e94586655
247
262
  - test/generators/component_generator_test.rb
248
- - test/support/foo.rb
263
+ - test/support/foo_component.mustache
264
+ - test/support/foo_component.rb
249
265
  - test/support/foo_controller.rb
250
266
  - test/test_helper.rb
251
267
  has_rdoc:
data/test/support/foo.rb DELETED
@@ -1,3 +0,0 @@
1
- class Foo < ActiveComponent::Base
2
- attr_accessor :bar
3
- end