rails-embryo 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
data/History.md CHANGED
@@ -1,3 +1,10 @@
1
+ ### Version 0.3.1
2
+ 2014-10-14
3
+
4
+ * Add controller generator
5
+ * Configure RSpec in zero monkey patching mode
6
+ * Bugfixes
7
+
1
8
  ### Version 0.3.0
2
9
  2014-09-14
3
10
 
data/README.md CHANGED
@@ -80,6 +80,18 @@ Authenticated models will automatically have an `email` field added and configur
80
80
  authentication key. To customize this or any other Devise behavior, edit the generated
81
81
  `config/initializers/devise.rb` along with the class and migration files for the new model.
82
82
 
83
+ #### Controllers
84
+
85
+ To generate a controller and an associated spec file, run the `embryo:controller` generator
86
+ with the name of the model.
87
+
88
+ rails generate embryo:controller user
89
+
90
+ The above command will generate the files `app/models/users_controller.rb` and
91
+ `spec/models/users_controller_spec.rb`. The generator assumes that the named model and other
92
+ related elements (migration, factory, view templates, and routes) already exist, so the
93
+ generated controller specs may fail if you haven't created those yet.
94
+
83
95
  ### Enhancements Added
84
96
 
85
97
  All of the following enhancements are added to new applications by
@@ -0,0 +1,44 @@
1
+ require "rails/generators"
2
+ require "rails-embryo/ruby_template/controller"
3
+ require "rails-embryo/ruby_template/controller_spec"
4
+ require "rails-embryo/ruby_template/action/index"
5
+ require "rails-embryo/ruby_template/action/new"
6
+ require "rails-embryo/ruby_template/action/create"
7
+ require "rails-embryo/ruby_template/action/show"
8
+ require "rails-embryo/ruby_template/action/update"
9
+ require "rails-embryo/ruby_template/action/destroy"
10
+ require "rails-embryo/ruby_template/model"
11
+
12
+ module Embryo
13
+ class ControllerGenerator < Rails::Generators::NamedBase
14
+ def install
15
+ create_file controller_template.path, controller_template.text
16
+ create_file spec_template.path, spec_template.text
17
+ end
18
+
19
+ private
20
+
21
+ def controller_template
22
+ @controller_template ||= RubyTemplate::Controller.new model, action_templates
23
+ end
24
+
25
+ def spec_template
26
+ @spec_template ||= RubyTemplate::ControllerSpec.new model, action_templates
27
+ end
28
+
29
+ def model
30
+ @model ||= RubyTemplate::Model.new name
31
+ end
32
+
33
+ def action_templates
34
+ [
35
+ RubyTemplate::Action::Index.new(model),
36
+ RubyTemplate::Action::New.new(model),
37
+ RubyTemplate::Action::Create.new(model),
38
+ RubyTemplate::Action::Show.new(model),
39
+ RubyTemplate::Action::Update.new(model),
40
+ RubyTemplate::Action::Destroy.new(model),
41
+ ]
42
+ end
43
+ end
44
+ end
@@ -33,7 +33,7 @@ end
33
33
  def controller_spec_data
34
34
  'require "rails_helper.rb"
35
35
 
36
- describe DashboardController do
36
+ RSpec.describe DashboardController do
37
37
  describe "#index" do
38
38
  it "succeeds" do
39
39
  get :index
@@ -47,7 +47,7 @@ end
47
47
  def feature_spec_data
48
48
  'require "rails_helper.rb"
49
49
 
50
- feature "Dashboard" do
50
+ RSpec.feature "Dashboard" do
51
51
  scenario "index view", :js do
52
52
  visit "/"
53
53
  expect(page).to have_content "Welcome"
@@ -22,7 +22,7 @@ end
22
22
  def spec_data
23
23
  'require "rails_helper"
24
24
 
25
- describe ' + class_name + ' do
25
+ RSpec.describe ' + class_name + ' do
26
26
  it "initializes successfully" do
27
27
  expect(build :' + factory_name + ').to be_valid
28
28
  end
@@ -5,7 +5,7 @@ module Embryo
5
5
  include GeneratorHelpers::Hidden
6
6
 
7
7
  def install
8
- gem "rspec-rails", "~> 3.0", group: :test
8
+ gem "rspec-rails", "~> 3.0", group: [:development, :test]
9
9
  create_file "spec/spec_helper.rb", spec_helper_data
10
10
  create_file "spec/rails_helper.rb", rails_helper_data
11
11
  remove_dir "test"
@@ -17,6 +17,7 @@ module Embryo
17
17
  'RSpec.configure do |config|
18
18
  config.color = true
19
19
  config.order = :random
20
+ config.disable_monkey_patching!
20
21
  config.mock_with :rspec do |mocks|
21
22
  mocks.verify_partial_doubles = true
22
23
  end
@@ -0,0 +1,25 @@
1
+ require "rails-embryo/ruby_template/action"
2
+
3
+ module Embryo
4
+ class RubyTemplate::Action::Create < RubyTemplate::Action
5
+ def controller_method_code
6
+ method_code :create,
7
+ "@#{model.singular} = #{model.class_name}.new #{model.singular}_params",
8
+ "if @#{model.singular}.save",
9
+ indent("redirect_to #{@model.plural_symbol}_path, notice: \"#{model.singular.capitalize} created.\""),
10
+ "else",
11
+ indent("render :new"),
12
+ "end"
13
+ end
14
+
15
+ def controller_spec_code
16
+ spec_group_code "#create",
17
+ spec_context_code("with valid attributes",
18
+ spec_before_code("post :create, #{model.singular}: attributes_for(:#{model.symbol})"),
19
+ spec_code("creates a new #{model.singular}",
20
+ "expect(assigns :#{model.singular}).to be_a #{model.class_name}"),
21
+ spec_code("redirects to the index on success",
22
+ "expect(response).to redirect_to #{model.plural_symbol}_path"))
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,27 @@
1
+ require "rails-embryo/ruby_template/action"
2
+
3
+ module Embryo
4
+ class RubyTemplate::Action::Destroy < RubyTemplate::Action
5
+ def controller_method_code
6
+ method_code :destroy,
7
+ "if @#{model.singular}.destroy",
8
+ indent("redirect_to #{@model.plural_symbol}_path, notice: \"#{model.singular.capitalize} deleted.\""),
9
+ "else",
10
+ indent("redirect_to #{@model.plural_symbol}_path, alert: \"Couldn't delete #{model.singular}.\""),
11
+ "end"
12
+ end
13
+
14
+ def controller_spec_code
15
+ spec_group_code "#destroy",
16
+ spec_context_code("when successful",
17
+ spec_before_code(
18
+ "@#{model.singular} = create :#{model.symbol}",
19
+ "delete :destroy, id: @#{model.singular}.id"
20
+ ),
21
+ spec_code("destroys the #{model.singular}",
22
+ "expect(#{model.class_name}.all).not_to include @#{model.singular}"),
23
+ spec_code("redirects to the index",
24
+ "expect(response).to redirect_to #{model.plural_symbol}_path"))
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,18 @@
1
+ require "rails-embryo/ruby_template/action"
2
+
3
+ module Embryo
4
+ class RubyTemplate::Action::Index < RubyTemplate::Action
5
+ def controller_method_code
6
+ method_code :index, "@#{model.plural} = #{model.class_name}.all"
7
+ end
8
+
9
+ def controller_spec_code
10
+ spec_group_code "#index",
11
+ spec_code("assigns the collection of #{model.plural}",
12
+ "#{model.singular} = create :#{model.symbol}",
13
+ "get :index",
14
+ "expect(assigns :#{model.plural}).to eq [#{model.singular}]"),
15
+ spec_code("succeeds", "get :index", "expect(response).to be_success")
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,17 @@
1
+ require "rails-embryo/ruby_template/action"
2
+
3
+ module Embryo
4
+ class RubyTemplate::Action::New < RubyTemplate::Action
5
+ def controller_method_code
6
+ method_code :new, "@#{model.singular} = #{model.class_name}.new"
7
+ end
8
+
9
+ def controller_spec_code
10
+ spec_group_code "#new",
11
+ spec_code("assigns a new #{model.singular}",
12
+ "get :new",
13
+ "expect(assigns :#{model.singular}).to be_a_new #{model.class_name}"),
14
+ spec_code("succeeds", "get :new", "expect(response).to be_success")
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,20 @@
1
+ require "rails-embryo/ruby_template/action"
2
+
3
+ module Embryo
4
+ class RubyTemplate::Action::Show < RubyTemplate::Action
5
+ def controller_method_code
6
+ method_code :show
7
+ end
8
+
9
+ def controller_spec_code
10
+ spec_group_code "#show",
11
+ spec_before_code(
12
+ "@#{model.singular} = create :#{model.symbol}",
13
+ "get :show, id: @#{model.singular}.id"),
14
+ spec_code("assigns the widget",
15
+ "expect(assigns :#{model.singular}).to eq @#{model.singular}"),
16
+ spec_code("succeeds",
17
+ "expect(response).to be_success")
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,23 @@
1
+ require "rails-embryo/ruby_template/action"
2
+
3
+ module Embryo
4
+ class RubyTemplate::Action::Update < RubyTemplate::Action
5
+ def controller_method_code
6
+ method_code :update,
7
+ "if @#{model.singular}.update_attributes #{model.singular}_params",
8
+ indent("redirect_to #{@model.plural_symbol}_path, notice: \"#{model.singular.capitalize} updated.\""),
9
+ "else",
10
+ indent("render :show"),
11
+ "end"
12
+ end
13
+
14
+ def controller_spec_code
15
+ spec_group_code "#update",
16
+ spec_context_code("with valid attributes",
17
+ spec_code("redirects to the index",
18
+ "@#{model.singular} = create :#{model.symbol}",
19
+ "post :update, id: @#{model.singular}.id, #{model.singular}: attributes_for(:#{model.symbol})",
20
+ "expect(response).to redirect_to #{model.plural_symbol}_path"))
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,31 @@
1
+ require "rails-embryo/ruby_template"
2
+
3
+ module Embryo
4
+ class RubyTemplate::Action < RubyTemplate
5
+ def initialize(model)
6
+ @model = model
7
+ end
8
+
9
+ protected
10
+
11
+ def model
12
+ @model
13
+ end
14
+
15
+ def spec_group_code(name, *specs)
16
+ ["describe #{name.inspect} do", *indent_separated(specs), "end"]
17
+ end
18
+
19
+ def spec_context_code(name, *specs)
20
+ ["context #{name.inspect} do", *indent_separated(specs), "end"]
21
+ end
22
+
23
+ def spec_before_code(*code)
24
+ ["before do", *indent(code), "end"]
25
+ end
26
+
27
+ def spec_code(name, *code)
28
+ ["it #{name.inspect} do", *indent(code), "end"]
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,44 @@
1
+ require "rails-embryo/ruby_template"
2
+
3
+ module Embryo
4
+ class RubyTemplate::Controller < RubyTemplate
5
+ def initialize(model, action_templates)
6
+ @model = model
7
+ @action_templates = action_templates
8
+ end
9
+
10
+ def path
11
+ "app/controllers/#{@model.plural_path}_controller.rb"
12
+ end
13
+
14
+ protected
15
+
16
+ def code
17
+ [
18
+ "class #{@model.plural_class_name}Controller < ApplicationController",
19
+ indent("before_action :find_#{@model.singular}, only: [:show, :update, :destroy]"),
20
+ *indented_action_methods,
21
+ indent_section("private"),
22
+ indent_section(finder_method),
23
+ indent_section(params_method),
24
+ "end"
25
+ ]
26
+ end
27
+
28
+ def indented_action_methods
29
+ @action_templates.map do |action|
30
+ indent_section action.controller_method_code
31
+ end
32
+ end
33
+
34
+ def finder_method
35
+ method_code "find_#{@model.singular}",
36
+ "@#{@model.singular} = #{@model.class_name}.find params[:id]"
37
+ end
38
+
39
+ def params_method
40
+ method_code "#{@model.singular}_params",
41
+ "params.fetch(:#{@model.singular})"
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,26 @@
1
+ require "rails-embryo/ruby_template"
2
+
3
+ module Embryo
4
+ class RubyTemplate::ControllerSpec < RubyTemplate
5
+ def initialize(model, action_templates)
6
+ @model = model
7
+ @action_templates = action_templates
8
+ end
9
+
10
+ def path
11
+ "spec/controllers/#{@model.plural_path}_controller_spec.rb"
12
+ end
13
+
14
+ protected
15
+
16
+ def code
17
+ [
18
+ "require 'rails_helper'",
19
+ "",
20
+ "RSpec.describe #{@model.plural_class_name}Controller do",
21
+ *indent_separated(@action_templates.map(&:controller_spec_code)),
22
+ "end"
23
+ ]
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,43 @@
1
+ require "active_support/core_ext/string"
2
+
3
+ module Embryo
4
+ class RubyTemplate
5
+ class Model
6
+ def initialize(name)
7
+ @name = name.singularize
8
+ end
9
+
10
+ def path
11
+ @name.underscore
12
+ end
13
+
14
+ def plural_path
15
+ path.pluralize
16
+ end
17
+
18
+ def singular
19
+ path.split("/").last
20
+ end
21
+
22
+ def plural
23
+ singular.pluralize
24
+ end
25
+
26
+ def class_name
27
+ @name.camelize
28
+ end
29
+
30
+ def plural_class_name
31
+ class_name.pluralize
32
+ end
33
+
34
+ def symbol
35
+ path.gsub("/", "_")
36
+ end
37
+
38
+ def plural_symbol
39
+ symbol.pluralize
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,39 @@
1
+ module Embryo
2
+ class RubyTemplate
3
+ def text
4
+ code.join("\n") + "\n"
5
+ end
6
+
7
+ protected
8
+
9
+ def indent(code)
10
+ if code.is_a? Array
11
+ code.map { |sub_code| indent sub_code }
12
+ elsif code == ""
13
+ ""
14
+ else
15
+ " " + code
16
+ end
17
+ end
18
+
19
+ def indent_section(code)
20
+ if code.is_a? Array
21
+ [""] + indent(code)
22
+ else
23
+ ["", indent(code)]
24
+ end
25
+ end
26
+
27
+ def indent_separated(code)
28
+ if code.is_a? Array
29
+ code.flat_map { |c| [indent(c), ""] }[0...-1]
30
+ else
31
+ indent code
32
+ end
33
+ end
34
+
35
+ def method_code(name, *code)
36
+ ["def #{name}", *indent(code), "end"]
37
+ end
38
+ end
39
+ end
@@ -1,5 +1,5 @@
1
1
  module Rails
2
2
  module Embryo
3
- VERSION = "0.3.0"
3
+ VERSION = "0.3.1"
4
4
  end
5
5
  end
data/lib/rails-embryo.rb CHANGED
@@ -2,6 +2,7 @@ require "rails-embryo/version"
2
2
  require "rails-embryo/generator_helpers/hidden"
3
3
  require "rails-embryo/generator_helpers/files"
4
4
  require "generators/embryo"
5
+ require "generators/embryo/controller"
5
6
  require "generators/embryo/model"
6
7
  require "generators/embryo/model/authenticated"
7
8
  require "rails/railtie"
metadata CHANGED
@@ -1,18 +1,20 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails-embryo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
+ prerelease:
5
6
  platform: ruby
6
7
  authors:
7
8
  - Brian Auton
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2014-09-14 00:00:00.000000000 Z
12
+ date: 2014-10-14 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: rails
15
16
  requirement: !ruby/object:Gem::Requirement
17
+ none: false
16
18
  requirements:
17
19
  - - "~>"
18
20
  - !ruby/object:Gem::Version
@@ -20,6 +22,7 @@ dependencies:
20
22
  type: :runtime
21
23
  prerelease: false
22
24
  version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
23
26
  requirements:
24
27
  - - "~>"
25
28
  - !ruby/object:Gem::Version
@@ -27,6 +30,7 @@ dependencies:
27
30
  - !ruby/object:Gem::Dependency
28
31
  name: rspec
29
32
  requirement: !ruby/object:Gem::Requirement
33
+ none: false
30
34
  requirements:
31
35
  - - "~>"
32
36
  - !ruby/object:Gem::Version
@@ -34,6 +38,7 @@ dependencies:
34
38
  type: :development
35
39
  prerelease: false
36
40
  version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
37
42
  requirements:
38
43
  - - "~>"
39
44
  - !ruby/object:Gem::Version
@@ -41,6 +46,7 @@ dependencies:
41
46
  - !ruby/object:Gem::Dependency
42
47
  name: rake
43
48
  requirement: !ruby/object:Gem::Requirement
49
+ none: false
44
50
  requirements:
45
51
  - - "~>"
46
52
  - !ruby/object:Gem::Version
@@ -48,6 +54,7 @@ dependencies:
48
54
  type: :development
49
55
  prerelease: false
50
56
  version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
51
58
  requirements:
52
59
  - - "~>"
53
60
  - !ruby/object:Gem::Version
@@ -60,48 +67,61 @@ executables:
60
67
  extensions: []
61
68
  extra_rdoc_files: []
62
69
  files:
63
- - History.md
64
- - License.txt
65
- - README.md
66
- - bin/rails-embryo
67
- - lib/generators/embryo.rb
70
+ - lib/rails-embryo/ruby_template/controller.rb
71
+ - lib/rails-embryo/ruby_template/controller_spec.rb
72
+ - lib/rails-embryo/ruby_template/model.rb
73
+ - lib/rails-embryo/ruby_template/action.rb
74
+ - lib/rails-embryo/ruby_template/action/create.rb
75
+ - lib/rails-embryo/ruby_template/action/new.rb
76
+ - lib/rails-embryo/ruby_template/action/destroy.rb
77
+ - lib/rails-embryo/ruby_template/action/show.rb
78
+ - lib/rails-embryo/ruby_template/action/index.rb
79
+ - lib/rails-embryo/ruby_template/action/update.rb
80
+ - lib/rails-embryo/generator_helpers/files.rb
81
+ - lib/rails-embryo/generator_helpers/hidden.rb
82
+ - lib/rails-embryo/version.rb
83
+ - lib/rails-embryo/ruby_template.rb
84
+ - lib/rails-embryo.rb
85
+ - lib/generators/embryo/controller.rb
86
+ - lib/generators/embryo/model/authenticated.rb
87
+ - lib/generators/embryo/factory_girl.rb
68
88
  - lib/generators/embryo/application.rb
69
89
  - lib/generators/embryo/capybara.rb
70
90
  - lib/generators/embryo/default_view.rb
71
- - lib/generators/embryo/devise.rb
72
- - lib/generators/embryo/factory_girl.rb
73
91
  - lib/generators/embryo/model.rb
74
- - lib/generators/embryo/model/authenticated.rb
75
- - lib/generators/embryo/poltergeist.rb
76
92
  - lib/generators/embryo/postgres.rb
77
- - lib/generators/embryo/rspec.rb
78
93
  - lib/generators/embryo/template_support.rb
79
- - lib/rails-embryo.rb
80
- - lib/rails-embryo/generator_helpers/files.rb
81
- - lib/rails-embryo/generator_helpers/hidden.rb
82
- - lib/rails-embryo/version.rb
94
+ - lib/generators/embryo/devise.rb
95
+ - lib/generators/embryo/poltergeist.rb
96
+ - lib/generators/embryo/rspec.rb
97
+ - lib/generators/embryo.rb
98
+ - README.md
99
+ - History.md
100
+ - License.txt
101
+ - bin/rails-embryo
83
102
  homepage: http://github.com/brianauton/rails-embryo
84
103
  licenses:
85
104
  - MIT
86
- metadata: {}
87
105
  post_install_message:
88
106
  rdoc_options: []
89
107
  require_paths:
90
108
  - lib
91
109
  required_ruby_version: !ruby/object:Gem::Requirement
110
+ none: false
92
111
  requirements:
93
112
  - - ">="
94
113
  - !ruby/object:Gem::Version
95
114
  version: '0'
96
115
  required_rubygems_version: !ruby/object:Gem::Requirement
116
+ none: false
97
117
  requirements:
98
118
  - - ">="
99
119
  - !ruby/object:Gem::Version
100
120
  version: 1.3.6
101
121
  requirements: []
102
122
  rubyforge_project:
103
- rubygems_version: 2.2.2
123
+ rubygems_version: 1.8.29
104
124
  signing_key:
105
- specification_version: 4
125
+ specification_version: 3
106
126
  summary: Generators for quick setup of advanced Rails practices
107
127
  test_files: []
checksums.yaml DELETED
@@ -1,7 +0,0 @@
1
- ---
2
- SHA1:
3
- metadata.gz: e3f4ad3fec63bed5cd7163c4e0324cdf91ff68a5
4
- data.tar.gz: f84aec31ccfaa312d3e3a11224770863e757c9dc
5
- SHA512:
6
- metadata.gz: e035eb9df84d3977804a7a9bacc5263bd4978bc234c0b87678a2ff80930332104adb5ef3e0497f7f3317200885637c4092d7df22e4e5c55f20bbb511f46e0768
7
- data.tar.gz: 10e4aa4adef14787d8c83983e84c1b0b177fac2bc9167a836ffed6c721859e713e925ff2338ab894ad80179bb8b0075f04ba4ac8925a2c336f3831a7f0bd40d7