modular 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (98) hide show
  1. data/.gitignore +2 -0
  2. data/.rvmrc +1 -0
  3. data/.travis.yml +2 -0
  4. data/Gemfile +14 -0
  5. data/Gemfile.lock +155 -0
  6. data/LICENSE +7 -0
  7. data/README.rdoc +21 -0
  8. data/Rakefile +14 -2
  9. data/TODO +3 -2
  10. data/config.ru +7 -0
  11. data/lib/modular.rb +15 -35
  12. data/lib/modular/action_controller_extension.rb +20 -0
  13. data/lib/modular/components.rb +6 -0
  14. data/lib/modular/components/base.rb +9 -7
  15. data/lib/modular/components/container.rb +1 -10
  16. data/lib/modular/components/main_content.rb +1 -3
  17. data/lib/modular/configuration.rb +10 -10
  18. data/lib/modular/creation.rb +29 -0
  19. data/lib/modular/{abstract_model.rb → helpers.rb} +0 -0
  20. data/lib/modular/layout_generator.rb +8 -7
  21. data/lib/modular/railtie.rb +11 -0
  22. data/lib/modular/rendering.rb +68 -73
  23. data/lib/modular/version.rb +1 -1
  24. data/modular.gemspec +0 -1
  25. data/{modular-app/spec → spec}/components/base.rb +0 -0
  26. data/{modular-app/spec → spec}/components/container.rb +3 -3
  27. data/{modular-app/spec → spec}/components/hello_world.rb +0 -0
  28. data/{modular-app/spec → spec}/components/helpers_example.rb +0 -0
  29. data/{modular-app/spec → spec}/components/validated.rb +0 -0
  30. data/{modular-app/spec → spec}/controllers/cached_for_time_controller_spec.rb +0 -0
  31. data/{modular-app/spec → spec}/controllers/cached_forever_controller_spec.rb +0 -0
  32. data/{modular-app/spec → spec}/controllers/callback_layout_controller_spec.rb +0 -0
  33. data/{modular-app/spec → spec}/controllers/example_controller_spec.rb +0 -0
  34. data/{modular-app/spec → spec}/controllers/indirect_render_controller_spec.rb +0 -0
  35. data/{modular-app/spec → spec}/controllers/layout_test_controller_spec.rb +1 -1
  36. data/{modular-app → spec/internal}/app/components/cached_for_time.rb +2 -4
  37. data/spec/internal/app/components/cached_forever.rb +8 -0
  38. data/spec/internal/app/components/fake_news_feed.rb +3 -0
  39. data/{modular-app → spec/internal}/app/components/foobar.rb +0 -0
  40. data/spec/internal/app/components/heavy_task.rb +8 -0
  41. data/{modular-app → spec/internal}/app/components/hello_world.rb +0 -0
  42. data/{modular-app → spec/internal}/app/components/helpers_example.rb +0 -0
  43. data/{modular-app → spec/internal}/app/components/validated.rb +0 -0
  44. data/{modular-app → spec/internal}/app/components/vertical.rb +0 -0
  45. data/{modular-app → spec/internal}/app/controllers/application_controller.rb +0 -0
  46. data/{modular-app → spec/internal}/app/controllers/cached_for_time_controller.rb +0 -0
  47. data/{modular-app → spec/internal}/app/controllers/cached_forever_controller.rb +0 -0
  48. data/{modular-app → spec/internal}/app/controllers/callback_layout_controller.rb +0 -0
  49. data/{modular-app → spec/internal}/app/controllers/example_controller.rb +0 -0
  50. data/{modular-app → spec/internal}/app/controllers/indirect_render_controller.rb +0 -0
  51. data/{modular-app → spec/internal}/app/controllers/layout_test_controller.rb +0 -0
  52. data/{modular-app → spec/internal}/app/models/simple_model.rb +0 -0
  53. data/{modular-app → spec/internal}/app/views/cached_for_time/index.html.erb +0 -0
  54. data/{modular-app → spec/internal}/app/views/cached_forever/index.html.erb +0 -0
  55. data/{modular-app → spec/internal}/app/views/callback_layout/index.html.erb +0 -0
  56. data/{modular-app → spec/internal}/app/views/components/cached_for_time.html.erb +0 -0
  57. data/{modular-app → spec/internal}/app/views/components/cached_forever.html.erb +0 -0
  58. data/{modular-app → spec/internal}/app/views/components/container.html.erb +0 -0
  59. data/{modular-app → spec/internal}/app/views/components/fake_news_feed.html.erb +0 -0
  60. data/{modular-app → spec/internal}/app/views/components/foobar.html.erb +0 -0
  61. data/{modular-app → spec/internal}/app/views/components/heavy_task.html.erb +0 -0
  62. data/{modular-app → spec/internal}/app/views/components/hello_world.html.erb +0 -0
  63. data/{modular-app → spec/internal}/app/views/components/helpers_example.html.erb +0 -0
  64. data/{modular-app → spec/internal}/app/views/components/validated.html.erb +0 -0
  65. data/{modular-app → spec/internal}/app/views/components/vertical.html.erb +0 -0
  66. data/{modular-app → spec/internal}/app/views/example/index.html.erb +0 -0
  67. data/{modular-app → spec/internal}/app/views/indirect_render/index.html.erb +0 -0
  68. data/{modular-app → spec/internal}/app/views/layout_test/index.html.erb +0 -0
  69. data/{modular-app → spec/internal}/app/views/layouts/application.html.erb +0 -0
  70. data/spec/internal/config/database.yml +3 -0
  71. data/{modular-app → spec/internal}/config/initializers/modular.rb +1 -1
  72. data/spec/internal/config/routes.rb +7 -0
  73. data/spec/internal/db/schema.rb +6 -0
  74. data/spec/internal/log/.gitignore +1 -0
  75. data/{README → spec/internal/public/favicon.ico} +0 -0
  76. data/spec/internal/tmp/.gitkeep +0 -0
  77. data/spec/internal/tmp/modular/cached_for_time.html.erb +14 -0
  78. data/spec/internal/tmp/modular/cached_forever.html.erb +12 -0
  79. data/spec/internal/tmp/modular/heavy.html.erb +19 -0
  80. data/spec/internal/tmp/modular/nested.html.erb +28 -0
  81. data/spec/modular/configuration_spec.rb +46 -0
  82. data/spec/{base.rb → modular/creation_spec.rb} +11 -16
  83. data/spec/spec_helper.rb +11 -3
  84. data/spec/views/indirect_render/index.html.erb_spec.rb +4 -0
  85. data/templates/layout.erb +5 -10
  86. metadata +119 -133
  87. data/autotest/discover.rb +0 -12
  88. data/lib/modular/rails.rb +0 -16
  89. data/modular-app/app/components/cached_forever.rb +0 -10
  90. data/modular-app/app/components/fake_news_feed.rb +0 -9
  91. data/modular-app/app/components/heavy_task.rb +0 -5
  92. data/modular-app/app/helpers/application_helper.rb +0 -2
  93. data/modular-app/app/helpers/callback_layout_helper.rb +0 -2
  94. data/modular-app/app/helpers/example_helper.rb +0 -2
  95. data/modular-app/app/helpers/indirect_render_helper.rb +0 -2
  96. data/modular-app/spec/helpers/indirect_render_helper_spec.rb +0 -14
  97. data/modular-app/spec/views/indirect_render/index.html.erb_spec.rb +0 -4
  98. data/spec/configuration.rb +0 -74
@@ -0,0 +1,29 @@
1
+ module Modular
2
+ module Creation
3
+ def create(typ, params = {})
4
+ return typ if typ.is_a? Modular::Components::Base
5
+
6
+ begin
7
+ begin
8
+ component = typ.to_s.camelize.constantize.new params
9
+ rescue NameError
10
+ component = (Components.name + '::' + typ.to_s.camelize).constantize.new params
11
+ end
12
+ rescue Exception => e
13
+ raise "Unable to create element " + typ.to_s
14
+ end
15
+
16
+ raise "Component has errors: " + component.errors.to_s if component.invalid?
17
+
18
+ component
19
+ end
20
+
21
+ def from_json(text)
22
+ obj = ActiveSupport::JSON.decode text
23
+ raise "Type expected in json string" unless obj['type']
24
+ create(obj['type'], obj.except('type'))
25
+ end
26
+
27
+ extend self
28
+ end
29
+ end
File without changes
@@ -3,7 +3,7 @@ require 'erb'
3
3
  module Modular
4
4
  class LayoutGenerator
5
5
  class ERBContext
6
- def initialize(hash)
6
+ def initialize(hash = {})
7
7
  hash.each_pair do |key, value|
8
8
  instance_variable_set('@' + key.to_s, value)
9
9
  end
@@ -23,7 +23,7 @@ module Modular
23
23
  filename.to_s
24
24
  end
25
25
 
26
- def initialize(id, params)
26
+ def initialize(id, params= {})
27
27
  @id = id
28
28
  @params = params
29
29
  end
@@ -38,7 +38,7 @@ module Modular
38
38
  end
39
39
 
40
40
  def layout_exists?
41
- if (defined? Rails) && (Rails.configuration.action_controller.perform_caching)
41
+ if Rails.configuration.action_controller.perform_caching
42
42
  File.exists?(filename)
43
43
  else
44
44
  false
@@ -48,19 +48,20 @@ module Modular
48
48
  def write_layout
49
49
  folder = self.foldername
50
50
  filename = self.filename
51
-
52
- Dir.mkdir(folder) unless File.exists? folder
51
+ path = "#{Rails.root}/#{folder}"
52
+
53
+ Dir.mkdir(path) unless File.exists? path
53
54
 
54
55
  template = ERB.new File.new(Gem.loaded_specs['modular'].full_gem_path + '/templates/layout.erb').read
55
56
  output = template.result(ERBContext.new(template_variables).get_binding)
56
57
 
57
- File.open(filename, 'w') do |f|
58
+ File.open("#{Rails.root}/#{filename}", 'w') do |f|
58
59
  f.write(output)
59
60
  end
60
61
  end
61
62
 
62
63
  def template_variables
63
- @params.merge :layout_id => @id
64
+ { :original_layout => 'application' }.merge(@params.merge(:layout_id => @id))
64
65
  end
65
66
  end
66
67
  end
@@ -0,0 +1,11 @@
1
+ require 'modular/action_controller_extension'
2
+
3
+ module TaggableCache
4
+ class Railtie < ::Rails::Railtie
5
+ initializer "modular" do |app|
6
+ ActiveSupport.on_load(:action_controller) do
7
+ ::ActionController::Base.__send__ :include, Modular::ActionControllerExtension
8
+ end
9
+ end
10
+ end
11
+ end
@@ -1,82 +1,77 @@
1
- module Modular
2
- module Components
3
- module DirectRender
4
- extend ActiveSupport::Concern
5
-
6
- module InstanceMethods
1
+ module Modular::Rendering
2
+ extend ActiveSupport::Concern
7
3
 
8
- def controller_path
9
- 'components'
10
- end
11
-
12
- def action_name
13
- type.underscore
14
- end
15
-
16
- def config
17
- ActionController::Base.config
18
- end
19
-
20
- def execute
21
-
22
- end
23
-
24
- def render
25
- execute
26
- modular_render :render_type => :direct
27
- end
28
-
29
- def initialize(attributes = {})
30
- append_view_path File.join('app', 'views')
31
- super
32
- end
33
-
34
- protected
35
- def modular_render(args = {})
36
- render_to_string :file => 'components/' + action_name, :locals => args
37
- end
38
- end
39
-
40
-
41
- module ClassMethods
42
- def view_context_class
43
- context = super
44
-
45
- context.send :define_method, :render_component do |component|
46
- if component.is_a?(Modular::Components::IndirectRender)
47
- component.indirect_render
48
- else
49
- a = "<%= Modular.from_json('"+ component.to_json + "').render %>"
50
- a.html_safe
51
- end
52
- end
53
-
54
- context.send :include, Rails.application.routes.url_helpers if defined? Rails
55
-
56
- context.send :include, ApplicationHelper if defined? ApplicationHelper
57
-
58
- context
59
- end
60
- end
4
+ def controller_path
5
+ 'components'
6
+ end
7
+
8
+ def action_name
9
+ type.underscore
10
+ end
61
11
 
62
- end
12
+ # def config
13
+ # ActionController::Base.config
14
+ # end
15
+
16
+ def execute
17
+ end
18
+
19
+ def render
20
+ execute
21
+ modular_render :render_type => :indirect
22
+ end
63
23
 
64
- module IndirectRender
65
- def execute
66
- end
67
-
68
- def indirect_render
69
- execute
70
- modular_render :render_type => :indirect
71
- end
24
+ # configure the different paths correctly
25
+ def initialize(*args)
26
+ super()
27
+ lookup_context.view_paths = Rails.root.join('app', 'views')
28
+ config.javascripts_dir = Rails.root.join('public', 'javascripts')
29
+ config.stylesheets_dir = Rails.root.join('public', 'stylesheets')
30
+ config.assets_dir = Rails.root.join('public')
31
+ end
32
+
33
+ # we are not in a browser, no need for this
34
+ def protect_against_forgery?
35
+ false
36
+ end
37
+
38
+ # so that your flash calls still work
39
+ def flash
40
+ {}
41
+ end
72
42
 
73
- def render(args = {})
74
- throw "Direct render unavailible for this component"
75
- end
43
+ def params
44
+ {}
45
+ end
46
+
47
+ # # same asset host as the controllers
48
+ # self.asset_host = ActionController::Base.asset_host
49
+
50
+ def initialize(attributes = {})
51
+ append_view_path(Rails.root.join('app', 'views'))
52
+ append_view_path(Gem.loaded_specs['modular'].full_gem_path + '/templates')
53
+
54
+ super
55
+ end
56
+
57
+ protected
58
+ def modular_render(args = {})
59
+ render_to_string :file => 'components/' + action_name, :locals => args
60
+ end
61
+
62
+ module ClassMethods
63
+ def view_context_class
64
+ context = super
76
65
 
77
- def self.included(base)
78
- thow "Unable to use indirect render without direct render" unless base.ancestors.include? Modular::Components::DirectRender
66
+ context.send :define_method, :render_component do |component|
67
+ component.render
79
68
  end
69
+
70
+ context.send :include, Rails.application.routes.url_helpers if defined? Rails
71
+
72
+ context.send :include, ApplicationHelper if defined? ApplicationHelper
73
+
74
+ context
80
75
  end
81
76
  end
82
77
  end
@@ -1,3 +1,3 @@
1
1
  module Modular
2
- VERSION = "0.1.2"
2
+ VERSION = "0.2.0"
3
3
  end
data/modular.gemspec CHANGED
@@ -21,5 +21,4 @@ Gem::Specification.new do |s|
21
21
 
22
22
  s.add_dependency "rails"
23
23
  s.add_development_dependency "rspec", ">= 2.0.0"
24
- s.add_development_dependency "autotest"
25
24
  end
File without changes
@@ -13,7 +13,7 @@ describe Modular::Components::Container, ' vertical container class ' do
13
13
  @cmp.components.push Modular.create(:FakeNewsFeed, { :news_count => 50, :title => 'Best news feed' }).to_json
14
14
  json = @cmp.to_json
15
15
  container = Modular.from_json(json)
16
- container.components[0].should be_a_kind_of Modular::Components::FakeNewsFeed
16
+ container.components[0].should be_a_kind_of FakeNewsFeed
17
17
  end
18
18
 
19
19
  it "should have add_container and add_child methods" do
@@ -26,9 +26,9 @@ describe Modular::Components::Container, ' vertical container class ' do
26
26
  @cmp.components[0].title.should == 'Test container title'
27
27
 
28
28
  feed = @cmp.components[0].components[0]
29
- feed.should be_a_kind_of Modular::Components::FakeNewsFeed
29
+ feed.should be_a_kind_of FakeNewsFeed
30
30
  feed.title.should == 'Worst news feed'
31
31
 
32
- @cmp.components[0].components[1].should be_a_kind_of Modular::Components::Vertical
32
+ @cmp.components[0].components[1].should be_a_kind_of Vertical
33
33
  end
34
34
  end
File without changes
@@ -3,7 +3,7 @@ require 'spec_helper'
3
3
  describe LayoutTestController do
4
4
  describe "prerequirements" do
5
5
  it "should render itself" do
6
- Modular.layout(:nested).indirect_render
6
+ Modular.layout(:nested).render
7
7
  end
8
8
  end
9
9
 
@@ -1,10 +1,8 @@
1
- class Modular::Components::CachedForTime < Modular::Components::Base
1
+ class CachedForTime < Modular::Components::Base
2
2
  include Modular::Components::Caching
3
3
  cache :expires_in => 2.seconds
4
4
 
5
5
  def execute
6
6
  SimpleModel.called_by_cached_for_time
7
7
  end
8
- end
9
-
10
- CachedForTime = Modular::Components::CachedForTime
8
+ end
@@ -0,0 +1,8 @@
1
+ class CachedForever < Modular::Components::Base
2
+ include Modular::Components::Caching
3
+ cache
4
+
5
+ def execute
6
+ SimpleModel.called_by_cached_forever
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ class FakeNewsFeed < Modular::Components::Base
2
+ attr_accessor :news_count
3
+ end
@@ -0,0 +1,8 @@
1
+ class Modular::Components::HeavyTask < Modular::Components::Base
2
+ include Modular::Rendering
3
+
4
+ def execute
5
+ end
6
+ end
7
+
8
+ HeavyTask = Modular::Components::HeavyTask
@@ -0,0 +1,3 @@
1
+ test:
2
+ adapter: sqlite3
3
+ database: db/combustion_test.sqlite
@@ -11,7 +11,7 @@ Modular.configure do
11
11
 
12
12
  register_layout :heavy do
13
13
  add Modular.layout(:simple)
14
- add :HeavyTask
14
+ add :heavy_task
15
15
  end
16
16
 
17
17
  register_layout :cached_forever, Modular.create(:CachedForever)
@@ -0,0 +1,7 @@
1
+ Rails.application.routes.draw do
2
+ get "cached_for_time/index"
3
+
4
+ root :to => "example#index"
5
+
6
+ match ':controller/(:action)'
7
+ end