decent_exposure 3.0.0.beta1 → 3.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. checksums.yaml +5 -5
  2. data/.travis.yml +12 -0
  3. data/CHANGELOG.md +19 -0
  4. data/Gemfile +2 -0
  5. data/README.md +58 -7
  6. data/decent_exposure.gemspec +16 -16
  7. data/gemfiles/Gemfile.rails-4.2.0 +5 -0
  8. data/gemfiles/Gemfile.rails-4.2.6 +5 -0
  9. data/gemfiles/Gemfile.rails-5.0.0 +5 -0
  10. data/lib/decent_exposure.rb +12 -5
  11. data/lib/decent_exposure/attribute.rb +0 -1
  12. data/lib/decent_exposure/behavior.rb +1 -1
  13. data/lib/decent_exposure/context.rb +2 -2
  14. data/lib/decent_exposure/controller.rb +4 -4
  15. data/lib/decent_exposure/exposure.rb +24 -23
  16. data/lib/decent_exposure/flow.rb +1 -2
  17. data/lib/decent_exposure/mailer.rb +15 -0
  18. data/lib/decent_exposure/version.rb +1 -1
  19. data/lib/generators/decent_exposure/USAGE +13 -0
  20. data/lib/generators/decent_exposure/scaffold_templates_generator.rb +46 -0
  21. data/lib/generators/decent_exposure/templates/_form.html.erb +34 -0
  22. data/lib/generators/decent_exposure/templates/_form.html.haml +15 -0
  23. data/lib/generators/decent_exposure/templates/controller.rb +41 -0
  24. data/lib/generators/decent_exposure/templates/edit.html.erb +6 -0
  25. data/lib/generators/decent_exposure/templates/edit.html.haml +7 -0
  26. data/lib/generators/decent_exposure/templates/index.html.erb +31 -0
  27. data/lib/generators/decent_exposure/templates/index.html.haml +25 -0
  28. data/lib/generators/decent_exposure/templates/new.html.erb +5 -0
  29. data/lib/generators/decent_exposure/templates/new.html.haml +5 -0
  30. data/lib/generators/decent_exposure/templates/show.html.erb +11 -0
  31. data/lib/generators/decent_exposure/templates/show.html.haml +11 -0
  32. data/spec/{controller_spec.rb → decent_exposure/controller_spec.rb} +45 -39
  33. data/spec/features/api_birds_controller_spec.rb +70 -0
  34. data/spec/features/birds_controller_spec.rb +70 -0
  35. data/spec/features/birds_mailer_spec.rb +54 -0
  36. data/spec/generators/decent_exposure/scaffold_templates_generator_spec.rb +45 -0
  37. data/spec/support/rails_app.rb +39 -7
  38. metadata +57 -35
  39. data/hashrocket_logo.png +0 -0
  40. data/spec/integration_spec.rb +0 -26
@@ -0,0 +1,70 @@
1
+ require "spec_helper"
2
+ require "support/rails_app"
3
+ require "rspec/rails"
4
+
5
+ RSpec.describe Api::BirdsController, type: :controller do
6
+ let(:bird) { Bird.new }
7
+
8
+ context "when birds relation is exposed" do
9
+ class Api::BirdsController
10
+ expose :birds, -> { Bird.all }
11
+ end
12
+
13
+ it "fetches all birds" do
14
+ expect(Bird).to receive(:all).and_return([bird])
15
+ get :index
16
+ expect(controller.birds).to eq([bird])
17
+ end
18
+ end
19
+
20
+ context "when a bird is exposed" do
21
+ class Api::BirdsController
22
+ expose :bird
23
+ end
24
+
25
+ it "finds model by id" do
26
+ expect(Bird).to receive(:find).with("bird-id").and_return(bird)
27
+ get :show, **request_params(id: "bird-id")
28
+ expect(controller.bird).to eq(bird)
29
+ end
30
+
31
+ it "finds model by bird_id" do
32
+ expect(Bird).to receive(:find).with("bird-id").and_return(bird)
33
+ get :new, **request_params(bird_id: "bird-id")
34
+ expect(controller.bird).to eq(bird)
35
+ end
36
+
37
+ it "builds bird if id is not provided" do
38
+ get :new
39
+ expect(controller.bird).to be_a(Bird)
40
+ end
41
+ end
42
+
43
+ context "when bird_params is defined" do
44
+ class Api::BirdsController
45
+ expose :bird
46
+
47
+ def bird_params
48
+ params.require(:bird).permit(:name)
49
+ end
50
+ end
51
+
52
+ it "bird is build with params set" do
53
+ post :create, **request_params(bird: {name: "crow"})
54
+ expect(controller.bird.name).to eq("crow")
55
+ end
56
+ end
57
+
58
+ context "when a bird? with a question mark is exposed" do
59
+ class Api::BirdsController
60
+ expose :bird
61
+ expose :bird?, -> { bird.present? }
62
+ end
63
+
64
+ it "exposes bird?" do
65
+ expect(Bird).to receive(:find).with("bird-id").and_return(bird)
66
+ get :show, **request_params(id: "bird-id")
67
+ expect(controller.bird?).to be true
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,70 @@
1
+ require "spec_helper"
2
+ require "support/rails_app"
3
+ require "rspec/rails"
4
+
5
+ RSpec.describe BirdsController, type: :controller do
6
+ let(:bird) { Bird.new }
7
+
8
+ context "when birds relation is exposed" do
9
+ class BirdsController
10
+ expose :birds, -> { Bird.all }
11
+ end
12
+
13
+ it "fetches all birds" do
14
+ expect(Bird).to receive(:all).and_return([bird])
15
+ get :index
16
+ expect(controller.birds).to eq([bird])
17
+ end
18
+ end
19
+
20
+ context "when a bird is exposed" do
21
+ class BirdsController
22
+ expose :bird
23
+ end
24
+
25
+ it "finds model by id" do
26
+ expect(Bird).to receive(:find).with("bird-id").and_return(bird)
27
+ get :show, **request_params(id: "bird-id")
28
+ expect(controller.bird).to eq(bird)
29
+ end
30
+
31
+ it "finds model by bird_id" do
32
+ expect(Bird).to receive(:find).with("bird-id").and_return(bird)
33
+ get :new, **request_params(bird_id: "bird-id")
34
+ expect(controller.bird).to eq(bird)
35
+ end
36
+
37
+ it "builds bird if id is not provided" do
38
+ get :new
39
+ expect(controller.bird).to be_a(Bird)
40
+ end
41
+ end
42
+
43
+ context "when bird_params is defined" do
44
+ class BirdsController
45
+ expose :bird
46
+
47
+ def bird_params
48
+ params.require(:bird).permit(:name)
49
+ end
50
+ end
51
+
52
+ it "bird is build with params set" do
53
+ post :create, **request_params(bird: {name: "crow"})
54
+ expect(controller.bird.name).to eq("crow")
55
+ end
56
+ end
57
+
58
+ context "when a bird? with a question mark is exposed" do
59
+ class BirdsController
60
+ expose :bird
61
+ expose :bird?, -> { bird.present? }
62
+ end
63
+
64
+ it "exposes bird?" do
65
+ expect(Bird).to receive(:find).with("bird-id").and_return(bird)
66
+ get :show, **request_params(id: "bird-id")
67
+ expect(controller.bird?).to be true
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,54 @@
1
+ require "spec_helper"
2
+ require "support/rails_app"
3
+ require "rspec/rails"
4
+ require "action_mailer"
5
+
6
+ class BirdsMailer < ActionMailer::Base
7
+ end
8
+
9
+ class BirdsMailer
10
+ expose(:birds, -> { Bird.all })
11
+ expose(:bird)
12
+
13
+ def hello_birds
14
+ mail { |format| format.text { render plain: "Hello #{birds}" } }
15
+ end
16
+
17
+ def hello_bird(id:)
18
+ mail { |format| format.text { render plain: "Hello #{bird}" } }
19
+ end
20
+
21
+ def hello_bird_by_id(id)
22
+ bird = Bird.find(id)
23
+ mail { |format| format.text { render plain: "Hello #{bird}" } }
24
+ end
25
+ end
26
+
27
+ RSpec.describe BirdsMailer, type: :mailer do
28
+ let(:bird) { double :bird }
29
+ let(:birds) { double :birds }
30
+
31
+ context "when birds relation is exposed" do
32
+ it "sends the email with exposed birds" do
33
+ expect(Bird).to receive(:all).and_return(birds)
34
+ expect(described_class.hello_birds.body.to_s)
35
+ .to include("Hello #{birds}")
36
+ end
37
+ end
38
+
39
+ context "when bird is exposed" do
40
+ it "sends the email with exposed bird" do
41
+ expect(Bird).to receive(:find).with("some-id").and_return(bird)
42
+ expect(described_class.hello_bird(id: "some-id").body.to_s)
43
+ .to include("Hello #{bird}")
44
+ end
45
+ end
46
+
47
+ context "with non hash argument" do
48
+ it "does not set params" do
49
+ expect(Bird).to receive(:find).with("some-id").and_return(bird)
50
+ expect(described_class.hello_bird_by_id("some-id").body.to_s)
51
+ .to include("Hello #{bird}")
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,45 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe DecentExposure::Generators::ScaffoldTemplatesGenerator, type: :generator do
4
+ subject(:generator) { described_class.new }
5
+
6
+ context "with erb" do
7
+ it "generates controller and erb views" do
8
+ allow(generator).to receive(:copy_file).with("controller.rb", "lib/templates/rails/scaffold_controller/controller.rb")
9
+ allow(generator).to receive(:copy_file).with("_form.html.erb", "lib/templates/erb/scaffold/_form.html.erb")
10
+ allow(generator).to receive(:copy_file).with("edit.html.erb", "lib/templates/erb/scaffold/edit.html.erb")
11
+ allow(generator).to receive(:copy_file).with("index.html.erb", "lib/templates/erb/scaffold/index.html.erb")
12
+ allow(generator).to receive(:copy_file).with("new.html.erb", "lib/templates/erb/scaffold/new.html.erb")
13
+ allow(generator).to receive(:copy_file).with("show.html.erb", "lib/templates/erb/scaffold/show.html.erb")
14
+
15
+ generator.generate
16
+ end
17
+ end
18
+
19
+ context "with haml" do
20
+ before do
21
+ allow(generator).to receive(:options).and_return(template_engine: :haml)
22
+ end
23
+
24
+ it "generates controller and haml views" do
25
+ allow(generator).to receive(:copy_file).with("controller.rb", "lib/templates/rails/scaffold_controller/controller.rb")
26
+ allow(generator).to receive(:copy_file).with("_form.html.haml", "lib/templates/haml/scaffold/_form.html.haml")
27
+ allow(generator).to receive(:copy_file).with("edit.html.haml", "lib/templates/haml/scaffold/edit.html.haml")
28
+ allow(generator).to receive(:copy_file).with("index.html.haml", "lib/templates/haml/scaffold/index.html.haml")
29
+ allow(generator).to receive(:copy_file).with("new.html.haml", "lib/templates/haml/scaffold/new.html.haml")
30
+ allow(generator).to receive(:copy_file).with("show.html.haml", "lib/templates/haml/scaffold/show.html.haml")
31
+
32
+ generator.generate
33
+ end
34
+ end
35
+
36
+ context "with invalid template_engine" do
37
+ before do
38
+ allow(generator).to receive(:options).and_return(template_engine: :foo_bar)
39
+ end
40
+
41
+ it "raises an ArgumentError" do
42
+ expect { generator.generate }. to raise_error(ArgumentError)
43
+ end
44
+ end
45
+ end
@@ -1,37 +1,69 @@
1
- require "active_support/all"
2
1
  require "action_controller"
3
- require "action_dispatch"
4
2
  require "rails"
5
3
 
4
+ def request_params(params)
5
+ return params if Rails::VERSION::MAJOR < 5
6
+ {params: params}
7
+ end
8
+
6
9
  module Rails
7
10
  class App
8
- def env_config; {} end
11
+ def env_config
12
+ {}
13
+ end
9
14
 
10
15
  def routes
11
16
  @routes ||= ActionDispatch::Routing::RouteSet.new.tap do |routes|
12
17
  routes.draw do
13
- resource :birds
18
+ resources :birds
19
+
20
+ namespace :api do
21
+ resources :birds
22
+ end
14
23
  end
15
24
  end
16
25
  end
17
26
  end
18
27
 
28
+ def self.root
29
+ ""
30
+ end
31
+
19
32
  def self.application
20
33
  @app ||= App.new
21
34
  end
22
35
  end
23
36
 
24
37
  class Bird
38
+ attr_accessor :name
39
+ def initialize(options = {})
40
+ options.each { |k, v| public_send("#{k}=", v) }
41
+ end
25
42
  end
26
43
 
27
44
  class ApplicationController < ActionController::Base
28
45
  include Rails.application.routes.url_helpers
29
46
  end
30
47
 
48
+ def base_api_class
49
+ return ApplicationController if Rails::VERSION::MAJOR < 5
50
+ ActionController::API
51
+ end
52
+
31
53
  class BirdsController < ApplicationController
32
- expose :bird
54
+ %i[index show edit new create update].each do |action|
55
+ define_method action do
56
+ head :ok
57
+ end
58
+ end
59
+ end
33
60
 
34
- def show
35
- head :ok
61
+ module Api
62
+ class BirdsController < base_api_class
63
+ %i[index show edit new create update].each do |action|
64
+ define_method action do
65
+ head :ok
66
+ end
67
+ end
36
68
  end
37
69
  end
metadata CHANGED
@@ -1,74 +1,74 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: decent_exposure
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.0.beta1
4
+ version: 3.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pavel Pravosud
8
8
  - Stephen Caudill
9
- autorequire:
9
+ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-05-08 00:00:00.000000000 Z
12
+ date: 2021-01-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: railties
15
+ name: activesupport
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  requirements:
18
- - - "~>"
18
+ - - ">="
19
19
  - !ruby/object:Gem::Version
20
- version: 5.x
20
+ version: '4.0'
21
21
  type: :runtime
22
22
  prerelease: false
23
23
  version_requirements: !ruby/object:Gem::Requirement
24
24
  requirements:
25
- - - "~>"
25
+ - - ">="
26
26
  - !ruby/object:Gem::Version
27
- version: 5.x
27
+ version: '4.0'
28
28
  - !ruby/object:Gem::Dependency
29
- name: activesupport
29
+ name: railties
30
30
  requirement: !ruby/object:Gem::Requirement
31
31
  requirements:
32
- - - "~>"
32
+ - - ">="
33
33
  - !ruby/object:Gem::Version
34
- version: 5.x
35
- type: :runtime
34
+ version: '4.0'
35
+ type: :development
36
36
  prerelease: false
37
37
  version_requirements: !ruby/object:Gem::Requirement
38
38
  requirements:
39
- - - "~>"
39
+ - - ">="
40
40
  - !ruby/object:Gem::Version
41
- version: 5.x
41
+ version: '4.0'
42
42
  - !ruby/object:Gem::Dependency
43
- name: rspec-rails
43
+ name: actionmailer
44
44
  requirement: !ruby/object:Gem::Requirement
45
45
  requirements:
46
- - - "~>"
46
+ - - ">="
47
47
  - !ruby/object:Gem::Version
48
- version: '3.0'
48
+ version: '0'
49
49
  type: :development
50
50
  prerelease: false
51
51
  version_requirements: !ruby/object:Gem::Requirement
52
52
  requirements:
53
- - - "~>"
53
+ - - ">="
54
54
  - !ruby/object:Gem::Version
55
- version: '3.0'
55
+ version: '0'
56
56
  - !ruby/object:Gem::Dependency
57
- name: rake
57
+ name: rspec-rails
58
58
  requirement: !ruby/object:Gem::Requirement
59
59
  requirements:
60
60
  - - "~>"
61
61
  - !ruby/object:Gem::Version
62
- version: '10.3'
62
+ version: '3.0'
63
63
  type: :development
64
64
  prerelease: false
65
65
  version_requirements: !ruby/object:Gem::Requirement
66
66
  requirements:
67
67
  - - "~>"
68
68
  - !ruby/object:Gem::Version
69
- version: '10.3'
69
+ version: '3.0'
70
70
  - !ruby/object:Gem::Dependency
71
- name: pry
71
+ name: standard
72
72
  requirement: !ruby/object:Gem::Requirement
73
73
  requirements:
74
74
  - - ">="
@@ -95,13 +95,16 @@ extra_rdoc_files: []
95
95
  files:
96
96
  - ".gitignore"
97
97
  - ".travis.yml"
98
+ - CHANGELOG.md
98
99
  - Gemfile
99
100
  - LICENSE.txt
100
101
  - README.md
101
102
  - Rakefile
102
103
  - decent_exposure.gemspec
103
104
  - decent_exposure.png
104
- - hashrocket_logo.png
105
+ - gemfiles/Gemfile.rails-4.2.0
106
+ - gemfiles/Gemfile.rails-4.2.6
107
+ - gemfiles/Gemfile.rails-5.0.0
105
108
  - lib/decent_exposure.rb
106
109
  - lib/decent_exposure/attribute.rb
107
110
  - lib/decent_exposure/behavior.rb
@@ -109,37 +112,56 @@ files:
109
112
  - lib/decent_exposure/controller.rb
110
113
  - lib/decent_exposure/exposure.rb
111
114
  - lib/decent_exposure/flow.rb
115
+ - lib/decent_exposure/mailer.rb
112
116
  - lib/decent_exposure/version.rb
113
- - spec/controller_spec.rb
114
- - spec/integration_spec.rb
117
+ - lib/generators/decent_exposure/USAGE
118
+ - lib/generators/decent_exposure/scaffold_templates_generator.rb
119
+ - lib/generators/decent_exposure/templates/_form.html.erb
120
+ - lib/generators/decent_exposure/templates/_form.html.haml
121
+ - lib/generators/decent_exposure/templates/controller.rb
122
+ - lib/generators/decent_exposure/templates/edit.html.erb
123
+ - lib/generators/decent_exposure/templates/edit.html.haml
124
+ - lib/generators/decent_exposure/templates/index.html.erb
125
+ - lib/generators/decent_exposure/templates/index.html.haml
126
+ - lib/generators/decent_exposure/templates/new.html.erb
127
+ - lib/generators/decent_exposure/templates/new.html.haml
128
+ - lib/generators/decent_exposure/templates/show.html.erb
129
+ - lib/generators/decent_exposure/templates/show.html.haml
130
+ - spec/decent_exposure/controller_spec.rb
131
+ - spec/features/api_birds_controller_spec.rb
132
+ - spec/features/birds_controller_spec.rb
133
+ - spec/features/birds_mailer_spec.rb
134
+ - spec/generators/decent_exposure/scaffold_templates_generator_spec.rb
115
135
  - spec/spec_helper.rb
116
136
  - spec/support/rails_app.rb
117
137
  homepage: https://github.com/hashrocket/decent_exposure
118
138
  licenses:
119
139
  - MIT
120
140
  metadata: {}
121
- post_install_message:
141
+ post_install_message:
122
142
  rdoc_options: []
123
143
  require_paths:
124
144
  - lib
125
145
  required_ruby_version: !ruby/object:Gem::Requirement
126
146
  requirements:
127
- - - "~>"
147
+ - - ">="
128
148
  - !ruby/object:Gem::Version
129
149
  version: '2.0'
130
150
  required_rubygems_version: !ruby/object:Gem::Requirement
131
151
  requirements:
132
- - - ">"
152
+ - - ">="
133
153
  - !ruby/object:Gem::Version
134
- version: 1.3.1
154
+ version: '0'
135
155
  requirements: []
136
- rubyforge_project:
137
- rubygems_version: 2.5.1
138
- signing_key:
156
+ rubygems_version: 3.2.3
157
+ signing_key:
139
158
  specification_version: 4
140
159
  summary: A helper for creating declarative interfaces in controllers
141
160
  test_files:
142
- - spec/controller_spec.rb
143
- - spec/integration_spec.rb
161
+ - spec/decent_exposure/controller_spec.rb
162
+ - spec/features/api_birds_controller_spec.rb
163
+ - spec/features/birds_controller_spec.rb
164
+ - spec/features/birds_mailer_spec.rb
165
+ - spec/generators/decent_exposure/scaffold_templates_generator_spec.rb
144
166
  - spec/spec_helper.rb
145
167
  - spec/support/rails_app.rb