rails3_plugin_toolbox 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -1,17 +1,10 @@
1
1
  # Rails3 plugin toolbox
2
2
 
3
- A toolbox to facilitate creating Plugins for Rails 3 without having to necessarily know a lot about the Rails 3 internals and Plugin architecture.
3
+ This is a small toolbox that greatly facilitates creating Plugins for Rails 3 without having to know a lot about the Rails 3 internals and Plugin architecture.
4
+ The toolbox provides a convenient DSL that abstracts away the internal Rails 3 plugin wiring to an even greater extent, making the DSL dead simple to use!
4
5
 
5
- ## RSpec test suite
6
-
7
- I have now discovered how to test he validity of Rails extensions in RSpec without having to have a full Rails 3 host app.
8
- RSpec 2 test suite will soon be added :)
9
-
10
- http://gist.github.com/542441
11
-
12
- ## Inspiration
13
-
14
- This project was inspired by the Yehuda Katz article at http://www.railsdispatch.com/, titled "How Rails enables more choices".
6
+ I also provide a specialized RSpec 2 matcher *be_extended_with* which makes it easy to spec your plugin extension functionality, to make sure Rails is extended with
7
+ the functionality (modules) that you expect and desire ;)
15
8
 
16
9
  ## Install
17
10
 
@@ -20,62 +13,106 @@ This project was inspired by the Yehuda Katz article at http://www.railsdispatch
20
13
  ## Usage
21
14
 
22
15
  <pre>
23
- # my_plugin/lib/load.rb
24
- module MyPlugin
25
- Rails3::PluginExtender.new do
26
-
27
- extend_from_module Ultra::View, :stuff, :in => :view
28
- extend_with Ultra::Controller, :in => :controller
29
-
30
- extend_rails :i18n do
31
- with MyAddition
32
- extend_from_module Ultra::Power, :util, :logging, :monitor
16
+ # my_plugin/lib/my_plugin/rails/extensions.rb
33
17
 
34
- before :initialize do
35
- MyOtherAddition.say 'before localized!'
36
- end
18
+ require 'rails/all'
19
+ require 'rails3_plugin_toolbox'
37
20
 
38
- before :configuration do
39
- MyOtherAddition.configured = 'was configured!'
40
- end
21
+ Rails3::PluginExtender.new do
22
+ extend_from_module Ultra::View, :stuff, :in => :view
23
+ extend_with Ultra::Controller, :in => :controller
41
24
 
42
- before :eager_load do
43
- puts "before eager load!"
44
- end
25
+ before :initialize do
26
+ "Rails not yet initialized!"
27
+ end
45
28
 
46
- after :initialize do
47
- MyAddition.say 'localized!'
48
- end
49
- end
29
+ extend_rails :i18n do
30
+ with MyAddition
31
+ extend_from_module Ultra::Power, :util, :logging, :monitor
50
32
 
51
- extend_rails :view do
52
- with MyViewAddition
53
- end
33
+ before :initialize do
34
+ MyOtherAddition.say 'before localized!'
35
+ end
54
36
 
55
- extend_rails :controller do
56
- with MyViewAddition
37
+ before :configuration do
38
+ MyOtherAddition.configured = 'was configured!'
57
39
  end
58
40
 
59
- extend_rails :mailer do
60
- with MyMailAddition, 'MyOtherMailAddition'
41
+ before :eager_load do
42
+ puts "before eager load!"
61
43
  end
62
44
 
63
- extend_rails :AR do
64
- with MyActiveRecordAddition
65
- end
45
+ after :initialize do
46
+ MyAddition.say 'localized!'
47
+ end
48
+ end
49
+ end
50
+ </pre>
51
+
52
+ More API examples
53
+
54
+ <pre>
55
+ Rails3::PluginExtender.new do
56
+ extend_rails :view do # or use :action_view
57
+ with MyViewAddition
58
+ end
59
+
60
+ extend_rails :controller do # or use :action_mailer
61
+ with MyViewAddition
62
+ end
63
+
64
+ extend_rails :mailer do # or use :action_mailer
65
+ with MyMailAddition, 'MyOtherMailAddition'
66
+ end
67
+
68
+ extend_rails :AR do # active record
69
+ with MyActiveRecordAddition
70
+ end
66
71
 
67
- extend_rails(:controller) do
68
- extend_from_module Ultra::Power, :util, :logging, :monitor
69
- extend_from_module Ultra::Power::More, :extra, :stuff
70
- end
71
-
72
- after(:initialize) do
73
- include MyCoreModule
74
- end
72
+ extend_rails :active_record do
73
+ with MyExtraScopeHelpers
75
74
  end
76
- end
75
+
76
+ extend_rails(:controller) do
77
+ extend_from_module Ultra::Power, :util, :logging, :monitor
78
+ extend_from_module Ultra::Power::More, :extra, :stuff
79
+ end
80
+
81
+ after(:initialize) do
82
+ puts "Rails initialized!"
83
+ end
84
+ end
77
85
  </pre>
78
86
 
87
+ ## RSpec 2 matchers
88
+
89
+ The library comes with a special Matcher to facilitate making Rails 3 extension specs
90
+
91
+ * be_extended_with module_name, *submodules
92
+
93
+ _Usage example:_
94
+
95
+ <pre>
96
+
97
+ require 'rspec'
98
+ require 'rails3_plugin_toolbox'
99
+ require 'rails/all'
100
+
101
+ it "should extend Action View with View Helpers" do
102
+ Rails3::PluginExtender.new do
103
+ extend_rails :view do
104
+ extend_from_module Helper::View, :panel, :window
105
+ extend_with Helper::View::Button, Helper::View::Form
106
+ end
107
+ end
108
+
109
+ # Initialize the rails application
110
+ Minimal::Application.initialize!
111
+
112
+ :view.should be_extended_with Helper::View, :panel, :window, :button, :form
113
+ :view.should_not be_extended_with Helper::View, :unknown
114
+ end
115
+ </pre>
79
116
 
80
117
  ## Note on Patches/Pull Requests
81
118
 
data/Rakefile CHANGED
@@ -8,8 +8,11 @@ begin
8
8
  gem.homepage = "http://github.com/kristianmandrup/rails3_plugin_toolbox"
9
9
  gem.authors = ["Kristian Mandrup"]
10
10
  gem.add_development_dependency "rspec", ">= 2.0.0.beta.19"
11
+
12
+ # gem.add_dependency 'active_support', ">= 3.0.0.rc"
11
13
  gem.add_dependency "rails", ">= 3.0.0.rc"
12
- gem.add_dependency "require_all", ">= 1.1.0"
14
+ gem.add_dependency "require_all", ">= 1.1.0"
15
+
13
16
  # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
14
17
  end
15
18
  Jeweler::GemcutterTasks.new
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.3.0
@@ -20,7 +20,7 @@ module Rails3
20
20
  def extend_from_module base_name, *module_names, options
21
21
  raise ArgumentError, "You must specify an options Hash as the last argument for #extend_from_module" if !options.kind_of? Hash
22
22
  module_names.each do |name|
23
- extend_with "#{base_name.camelize}::#{name.to_s.camelize}".constantize, options
23
+ extend_with get_constant(base_name, name), options
24
24
  end
25
25
  end
26
26
 
@@ -38,7 +38,7 @@ module Rails3
38
38
 
39
39
  def extend_from_module base_name, *module_names
40
40
  module_names.each do |name|
41
- include "#{base_name.camelize}::#{name.to_s.camelize}".constantize
41
+ include get_constant(base_name, name)
42
42
  end
43
43
  end
44
44
 
@@ -0,0 +1,8 @@
1
+ require 'rspec'
2
+
3
+ RSpec.configure do |config|
4
+ config.include Rails3::PluginExtender::Matchers
5
+ end
6
+
7
+
8
+
@@ -0,0 +1,58 @@
1
+ module Rails3
2
+ class PluginExtender
3
+ module Matchers
4
+ class BeExtendedWith
5
+ include Rails3::PluginExtender::Util
6
+
7
+ attr_reader :methods, :module_const, :rails_const, :submodules
8
+
9
+ def initialize(module_const, submodules=nil)
10
+ @module_const = module_const
11
+ raise ArgumentError, "List of submodules must be given as a collection of Symbols or Strings" if submodules && !submodules.respond_to?(:flatten)
12
+ @submodules = submodules.flatten if submodules
13
+ end
14
+
15
+ def matches? type
16
+ begin
17
+ @rails_const = get_base_class(type)
18
+ return match_submodules? if submodules
19
+ methods_included? module_const.instance_methods
20
+ rescue
21
+ false
22
+ end
23
+ end
24
+
25
+ def base_class_methods
26
+ (rails_const == I18n) ? rails_const.methods : rails_const.instance_methods
27
+ end
28
+
29
+ def match_submodules?
30
+ submodules.each do |name|
31
+ return false if !methods_included? get_methods(name)
32
+ end
33
+ true
34
+ end
35
+
36
+ def methods_included? methods
37
+ (base_class_methods & methods) == methods
38
+ end
39
+
40
+ def get_methods name
41
+ get_constant(module_const, name).instance_methods
42
+ end
43
+
44
+ def failure_message
45
+ "Expected the rails class #{rails_const} to be extended with all the methods in #{module_const}"
46
+ end
47
+
48
+ def negative_failure_message
49
+ "Din not expect the rails class #{rails_const} to be extended with all the methods in #{module_const}"
50
+ end
51
+ end
52
+
53
+ def be_extended_with(module_const, *submodules)
54
+ BeExtendedWith.new module_const, submodules
55
+ end
56
+ end
57
+ end
58
+ end
@@ -3,7 +3,17 @@ module Rails3
3
3
  module Util
4
4
  INIT = :initialize
5
5
  ACTIVE_MODULES = {:AR => :active_record, :view => :action_view, :controller => :action_controller, :mailer => :action_mailer}
6
-
6
+
7
+ def get_base_class type
8
+ type = get_load_type(type).to_s
9
+ return "#{type.to_s.camelize}::Base".constantize if type =~/action/ || type =~/active/
10
+ "#{type.to_s.camelize}".constantize
11
+ end
12
+
13
+ def get_constant base_name, name
14
+ "#{base_name.to_s.camelize}::#{name.to_s.camelize}".constantize
15
+ end
16
+
7
17
  def get_load_type type
8
18
  return ACTIVE_MODULES[type] if ACTIVE_MODULES[type]
9
19
  return type if ACTIVE_MODULES.values.include? type
@@ -1,5 +1,7 @@
1
1
  module Rails3
2
2
  end
3
3
 
4
- require 'require_all'
4
+ require 'active_support/inflector'
5
+ require 'require_all'
6
+
5
7
  require_all File.dirname(__FILE__) + '/plugin_toolbox'
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{rails3_plugin_toolbox}
8
- s.version = "0.2.0"
8
+ s.version = "0.3.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Kristian Mandrup"]
@@ -27,10 +27,13 @@ Gem::Specification.new do |s|
27
27
  "config/database.yml",
28
28
  "lib/plugin_toolbox/extender.rb",
29
29
  "lib/plugin_toolbox/loader.rb",
30
+ "lib/plugin_toolbox/rspec/config.rb",
31
+ "lib/plugin_toolbox/rspec/matchers/be_extended_with.rb",
30
32
  "lib/plugin_toolbox/util.rb",
31
33
  "lib/rails3_plugin_toolbox.rb",
32
34
  "log/development.log",
33
35
  "rails3_plugin_toolbox.gemspec",
36
+ "spec/plugin_toolbox/extend_view_content_spec.rb",
34
37
  "spec/plugin_toolbox/extender_action_spec.rb",
35
38
  "spec/plugin_toolbox/extender_i18n_spec.rb",
36
39
  "spec/plugin_toolbox/extender_spec.rb",
@@ -45,7 +48,8 @@ Gem::Specification.new do |s|
45
48
  s.rubygems_version = %q{1.3.7}
46
49
  s.summary = %q{Toolbox to facilitate Rails 3 plugin development}
47
50
  s.test_files = [
48
- "spec/plugin_toolbox/extender_action_spec.rb",
51
+ "spec/plugin_toolbox/extend_view_content_spec.rb",
52
+ "spec/plugin_toolbox/extender_action_spec.rb",
49
53
  "spec/plugin_toolbox/extender_i18n_spec.rb",
50
54
  "spec/plugin_toolbox/extender_spec.rb",
51
55
  "spec/spec_helper.rb"
@@ -0,0 +1,48 @@
1
+ require 'spec_helper'
2
+
3
+ module Helper
4
+ module View
5
+ module Panel
6
+ def draw_panel
7
+ 'panel'
8
+ end
9
+ end
10
+
11
+ module Window
12
+ def draw_window
13
+ 'panel'
14
+ end
15
+ end
16
+
17
+ module Button
18
+ def draw_button
19
+ 'button'
20
+ end
21
+ end
22
+
23
+ module Form
24
+ def draw_button
25
+ 'button'
26
+ end
27
+ end
28
+ end
29
+ end
30
+
31
+ describe Rails3::PluginExtender do
32
+ describe '#extend_rails' do
33
+ it "should extend Action View" do
34
+ Rails3::PluginExtender.new do
35
+ extend_rails :view do
36
+ extend_from_module Helper::View, :panel, :window
37
+ extend_with Helper::View::Button, Helper::View::Form
38
+ end
39
+ end
40
+
41
+ # Initialize the rails application
42
+ Minimal::Application.initialize!
43
+
44
+ :view.should be_extended_with Helper::View, :panel, :window, :button, :form
45
+ :view.should_not be_extended_with Helper::View, :unknown
46
+ end
47
+ end
48
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,13 +1,14 @@
1
1
  require 'rspec'
2
2
  require 'rspec/autorun'
3
- require 'active_support'
4
- require 'active_record'
5
- require 'action_controller'
6
- require 'action_view'
7
3
  require 'rails3_plugin_toolbox'
8
4
 
9
5
  # See http://www.igvita.com/2010/08/04/rails-3-internals-railtie-creating-plugins/
10
6
 
7
+ # require 'active_support'
8
+ # require 'active_record'
9
+ # require 'action_controller'
10
+ # require 'action_view'
11
+ # require 'action_mailer'
11
12
  # require 'active_support/railtie'
12
13
  require 'rails/all'
13
14
 
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 2
7
+ - 3
8
8
  - 0
9
- version: 0.2.0
9
+ version: 0.3.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Kristian Mandrup
@@ -85,10 +85,13 @@ files:
85
85
  - config/database.yml
86
86
  - lib/plugin_toolbox/extender.rb
87
87
  - lib/plugin_toolbox/loader.rb
88
+ - lib/plugin_toolbox/rspec/config.rb
89
+ - lib/plugin_toolbox/rspec/matchers/be_extended_with.rb
88
90
  - lib/plugin_toolbox/util.rb
89
91
  - lib/rails3_plugin_toolbox.rb
90
92
  - log/development.log
91
93
  - rails3_plugin_toolbox.gemspec
94
+ - spec/plugin_toolbox/extend_view_content_spec.rb
92
95
  - spec/plugin_toolbox/extender_action_spec.rb
93
96
  - spec/plugin_toolbox/extender_i18n_spec.rb
94
97
  - spec/plugin_toolbox/extender_spec.rb
@@ -129,6 +132,7 @@ signing_key:
129
132
  specification_version: 3
130
133
  summary: Toolbox to facilitate Rails 3 plugin development
131
134
  test_files:
135
+ - spec/plugin_toolbox/extend_view_content_spec.rb
132
136
  - spec/plugin_toolbox/extender_action_spec.rb
133
137
  - spec/plugin_toolbox/extender_i18n_spec.rb
134
138
  - spec/plugin_toolbox/extender_spec.rb