aws-record-generator 1.0.0.pre.1 → 1.0.0.pre.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (25) hide show
  1. checksums.yaml +4 -4
  2. data/lib/aws-record-generator.rb +6 -0
  3. data/lib/generators/aws_record/active_model.rb +80 -0
  4. data/lib/generators/aws_record/base.rb +228 -0
  5. data/lib/generators/aws_record/erb/erb_generator.rb +67 -0
  6. data/lib/generators/aws_record/erb/templates/_form.html.erb.tt +34 -0
  7. data/lib/generators/aws_record/erb/templates/edit.html.erb.tt +6 -0
  8. data/lib/generators/aws_record/erb/templates/index.html.erb.tt +31 -0
  9. data/lib/generators/aws_record/erb/templates/new.html.erb.tt +5 -0
  10. data/lib/generators/aws_record/erb/templates/show.html.erb.tt +11 -0
  11. data/lib/generators/aws_record/model/USAGE +3 -0
  12. data/lib/generators/aws_record/model/model_generator.rb +7 -200
  13. data/lib/generators/aws_record/model/templates/model.rb.tt +47 -6
  14. data/lib/generators/aws_record/resource/USAGE +23 -0
  15. data/lib/generators/aws_record/resource/resource_generator.rb +31 -0
  16. data/lib/generators/aws_record/scaffold/USAGE +31 -0
  17. data/lib/generators/aws_record/scaffold/scaffold_generator.rb +56 -0
  18. data/lib/generators/aws_record/scaffold_controller/USAGE +15 -0
  19. data/lib/generators/aws_record/scaffold_controller/scaffold_controller_generator.rb +60 -0
  20. data/lib/generators/aws_record/scaffold_controller/templates/api_controller.rb.tt +63 -0
  21. data/lib/generators/aws_record/scaffold_controller/templates/controller.rb.tt +70 -0
  22. data/lib/generators/generated_attribute.rb +32 -0
  23. data/lib/generators/test_helper.rb +17 -6
  24. data/lib/tasks/table_config_migrate_task.rake +1 -1
  25. metadata +20 -4
@@ -0,0 +1,15 @@
1
+ Description:
2
+ Stubs out a scaffolded controller, its seven RESTful actions and related
3
+ views. Pass the model name, either CamelCased or under_scored. The
4
+ controller name is retrieved as a pluralized version of the model name.
5
+
6
+ This generates a controller class in app/controllers and invokes helper,
7
+ template engine framework generators.
8
+
9
+ Example:
10
+ `rails generate scaffold_controller CreditCard...`
11
+
12
+ Credit card controller with URLs like /credit_cards.
13
+ Controller: app/controllers/credit_cards_controller.rb
14
+ Views: app/views/credit_cards/index.html.erb [...]
15
+ Helper: app/helpers/credit_cards_helper.rb
@@ -0,0 +1,60 @@
1
+ # Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License"). You may not
4
+ # use this file except in compliance with the License. A copy of the License is
5
+ # located at
6
+ #
7
+ # http://aws.amazon.com/apache2.0/
8
+ #
9
+ # or in the "license" file accompanying this file. This file is distributed on
10
+ # an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11
+ # or implied. See the License for the specific language governing permissions
12
+ # and limitations under the License.
13
+
14
+ require "rails/generators/resource_helpers"
15
+ require 'generators/aws_record/active_model'
16
+
17
+ module AwsRecord
18
+ module Generators
19
+ class ScaffoldControllerGenerator < Base
20
+ include Rails::Generators::ResourceHelpers
21
+ source_root File.expand_path('../templates', __FILE__)
22
+
23
+ check_class_collision suffix: "Controller"
24
+
25
+ class_option :helper, type: :boolean
26
+ class_option :orm, banner: "NAME", type: :string, required: true,
27
+ desc: "ORM to generate the controller for"
28
+ class_option :api, type: :boolean,
29
+ desc: "Generates API controller"
30
+
31
+ argument :attributes, type: :array, default: [], banner: "field:type field:type"
32
+
33
+ def initialize(args, *options)
34
+ options[0] << "--skip-table-config"
35
+ super
36
+ end
37
+
38
+ def create_controller_files
39
+ template_file = options.api? ? "api_controller.rb" : "controller.rb"
40
+ template template_file, File.join("app/controllers", controller_class_path, "#{controller_file_name}_controller.rb")
41
+ end
42
+
43
+ hook_for :template_engine, in: :aws_record do |template_engine|
44
+ invoke template_engine unless options.api?
45
+ end
46
+
47
+ hook_for :test_framework, as: :scaffold
48
+
49
+ # Invoke the helper using the controller name (pluralized)
50
+ hook_for :helper, as: :scaffold, in: :rails do |invoked|
51
+ invoke invoked, [ controller_name ]
52
+ end
53
+
54
+ private
55
+ def orm_class
56
+ @orm_class = AwsRecord::Generators::ActiveModel
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,63 @@
1
+ <% if namespaced? -%>
2
+ require_dependency "<%= namespaced_path %>/application_controller"
3
+
4
+ <% end -%>
5
+ <% module_namespacing do -%>
6
+ class <%= controller_class_name %>Controller < ApplicationController
7
+ before_action :set_<%= singular_table_name %>, only: [:show, :update, :destroy]
8
+
9
+ # GET <%= route_url %>
10
+ def index
11
+ # Warning: This performs a full table scan, which can be very expensive if a table has many entries.
12
+ # It is strongly recommended to implement alternative approaches such as paginated queries.
13
+ @<%= plural_table_name %> = <%= orm_class.all(class_name) %>
14
+
15
+ render json: <%= "@#{plural_table_name}" %>
16
+ end
17
+
18
+ # GET <%= route_url %>/1
19
+ def show
20
+ render json: <%= "@#{singular_table_name}" %>
21
+ end
22
+
23
+ # POST <%= route_url %>
24
+ def create
25
+ @<%= singular_table_name %> = <%= orm_class.build(class_name, "#{singular_table_name}_params") %>
26
+
27
+ if @<%= orm_instance.save %>
28
+ render json: <%= "@#{singular_table_name}" %>, status: :created, location: <%= "@#{singular_table_name}" %>
29
+ else
30
+ render json: <%= "@#{orm_instance.errors}" %>, status: :unprocessable_entity
31
+ end
32
+ end
33
+
34
+ # PATCH/PUT <%= route_url %>/1
35
+ def update
36
+ if @<%= orm_instance.update("#{singular_table_name}_params") %>
37
+ render json: <%= "@#{singular_table_name}" %>
38
+ else
39
+ render json: <%= "@#{orm_instance.errors}" %>, status: :unprocessable_entity
40
+ end
41
+ end
42
+
43
+ # DELETE <%= route_url %>/1
44
+ def destroy
45
+ @<%= orm_instance.destroy %>
46
+ end
47
+
48
+ private
49
+ # Use callbacks to share common setup or constraints between actions.
50
+ def set_<%= singular_table_name %>
51
+ @<%= singular_table_name %> = <%= orm_class.find(class_name, attributes) %>
52
+ end
53
+
54
+ # Only allow a trusted parameter "white list" through.
55
+ def <%= "#{singular_table_name}_params" %>
56
+ <%- if attributes_names.empty? -%>
57
+ params.fetch(:<%= singular_table_name %>, {})
58
+ <%- else -%>
59
+ params.require(:<%= singular_table_name %>).permit(<%= attributes_names.map { |name| ":#{name}" }.join(', ') %>)
60
+ <%- end -%>
61
+ end
62
+ end
63
+ <% end -%>
@@ -0,0 +1,70 @@
1
+ <% if namespaced? -%>
2
+ require_dependency "<%= namespaced_path %>/application_controller"
3
+
4
+ <% end -%>
5
+ <% module_namespacing do -%>
6
+ class <%= controller_class_name %>Controller < ApplicationController
7
+ before_action :set_<%= singular_table_name %>, only: [:show, :edit, :update, :destroy]
8
+
9
+ # GET <%= route_url %>
10
+ def index
11
+ # Warning: This performs a full table scan, which can be very expensive if a table has many entries.
12
+ # It is strongly recommended to implement alternative approaches such as paginated queries.
13
+ @<%= plural_table_name %> = <%= orm_class.all(class_name) %>
14
+ end
15
+
16
+ # GET <%= route_url %>/1
17
+ def show
18
+ end
19
+
20
+ # GET <%= route_url %>/new
21
+ def new
22
+ @<%= singular_table_name %> = <%= orm_class.build(class_name) %>
23
+ end
24
+
25
+ # GET <%= route_url %>/1/edit
26
+ def edit
27
+ end
28
+
29
+ # POST <%= route_url %>
30
+ def create
31
+ @<%= singular_table_name %> = <%= orm_class.build(class_name, "#{singular_table_name}_params") %>
32
+
33
+ if @<%= orm_instance.save %>
34
+ redirect_to <%= redirect_resource_name %>, notice: <%= "'#{human_name} was successfully created.'" %>
35
+ else
36
+ render :new
37
+ end
38
+ end
39
+
40
+ # PATCH/PUT <%= route_url %>/1
41
+ def update
42
+ if @<%= orm_instance.update("#{singular_table_name}_params") %>
43
+ redirect_to <%= redirect_resource_name %>, notice: <%= "'#{human_name} was successfully updated.'" %>
44
+ else
45
+ render :edit
46
+ end
47
+ end
48
+
49
+ # DELETE <%= route_url %>/1
50
+ def destroy
51
+ @<%= orm_instance.destroy %>
52
+ redirect_to <%= index_helper %>_url, notice: <%= "'#{human_name} was successfully destroyed.'" %>
53
+ end
54
+
55
+ private
56
+ # Use callbacks to share common setup or constraints between actions.
57
+ def set_<%= singular_table_name %>
58
+ @<%= singular_table_name %> = <%= orm_class.find(class_name, attributes) %>
59
+ end
60
+
61
+ # Only allow a trusted parameter "white list" through.
62
+ def <%= "#{singular_table_name}_params" %>
63
+ <%- if attributes_names.empty? -%>
64
+ params.fetch(:<%= singular_table_name %>, {})
65
+ <%- else -%>
66
+ params.require(:<%= singular_table_name %>).permit(<%= attributes_names.map { |name| ":#{name}" }.join(', ') %>)
67
+ <%- end -%>
68
+ end
69
+ end
70
+ <% end -%>
@@ -20,6 +20,16 @@ module AwsRecord
20
20
  attr_reader :name, :type
21
21
  attr_accessor :options
22
22
 
23
+ def field_type
24
+ case @type
25
+ when :integer_attr then :number_field
26
+ when :date_attr then :date_select
27
+ when :datetime_attr then :datetime_select
28
+ when :boolean_attr then :check_box
29
+ else :text_field
30
+ end
31
+ end
32
+
23
33
  class << self
24
34
 
25
35
  def parse(field_definition)
@@ -104,6 +114,28 @@ module AwsRecord
104
114
  @name = name
105
115
  @type = type
106
116
  @options = options
117
+ @digest = options.delete(:digest)
118
+ end
119
+
120
+ # Methods used by rails scaffolding
121
+ def password_digest?
122
+ @digest
123
+ end
124
+
125
+ def polymorphic?
126
+ false
127
+ end
128
+
129
+ def column_name
130
+ if @name == "password_digest"
131
+ "password"
132
+ else
133
+ @name
134
+ end
135
+ end
136
+
137
+ def human_name
138
+ name.humanize
107
139
  end
108
140
  end
109
141
  end
@@ -11,8 +11,9 @@
11
11
  # or implied. See the License for the specific language governing permissions
12
12
  # and limitations under the License.
13
13
 
14
- require 'rails/generators/rails/app/app_generator'
14
+ require "rails/generators/rails/app/app_generator"
15
15
  require "rails/generators/testing/behaviour"
16
+ require "rails/generators/testing/assertions"
16
17
  require "fileutils"
17
18
  require "minitest/spec"
18
19
 
@@ -23,6 +24,7 @@ module AwsRecord
23
24
  attr_accessor :assertions
24
25
 
25
26
  include Rails::Generators::Testing::Behaviour
27
+ include Rails::Generators::Testing::Assertions
26
28
  include FileUtils
27
29
 
28
30
  def initialize(klass, dest)
@@ -47,14 +49,21 @@ module AwsRecord
47
49
  ensure_current_path
48
50
  end
49
51
 
50
- def assert_file(generated_file, actual_file)
52
+ def assert_file_fixture(generated_file, actual_file)
51
53
  assert File.exist?(generated_file), "Expected file #{generated_file.inspect} to exist, but does not"
52
54
  assert File.exist?(actual_file), "Expected file #{actual_file.inspect} to exist, but does not"
53
55
  assert identical? generated_file, actual_file
54
56
  end
55
57
 
56
- def assert_not_file(file_path)
57
- assert !File.exist?(file_path), "Expected file #{file_path.inspect} to not exist, but does"
58
+ def assert_model_rand_table_name(generated_file, actual_file, table_name)
59
+ assert File.exist?(generated_file), "Expected file #{generated_file.inspect} to exist, but does not"
60
+ assert File.exist?(actual_file), "Expected file #{actual_file.inspect} to exist, but does not"
61
+
62
+ fixture = File.read(actual_file)
63
+ generated = File.read(generated_file)
64
+ fixture = fixture.gsub(/#table_name#/, table_name)
65
+
66
+ assert fixture == generated
58
67
  end
59
68
 
60
69
  def cleanup
@@ -62,15 +71,17 @@ module AwsRecord
62
71
  end
63
72
 
64
73
  def run_generator(args = default_arguments, config = {})
74
+ result = nil
65
75
  capture(:stderr) do
66
- super
76
+ result = super
67
77
  end
78
+ result
68
79
  end
69
80
 
70
81
  private
71
82
 
72
83
  def setup_test_app
73
- Rails::Generators::AppGenerator.start [destination_root, '--skip-bundle', '--skip-git', '--skip-spring', '--skip-test', '-d' , '--skip-javascript', '--force', '--quiet']
84
+ Rails::Generators::AppGenerator.start [destination_root, '--skip-bundle', '--skip-git', '--skip-spring', '--skip-test', '--force', '--quiet']
74
85
  `echo 'gem "aws-record-generator", :path => "../../"' >> "#{destination_root}/Gemfile"`
75
86
  `bundle install --gemfile "#{destination_root}/Gemfile"`
76
87
  end
@@ -17,7 +17,7 @@ namespace :aws_record do
17
17
  task migrate: :environment do
18
18
  Dir[File.join('db', 'table_config', '**/*.rb')].each do |filename|
19
19
  puts "running #{filename}"
20
- load(filename)
20
+ require (File.expand_path(filename))
21
21
 
22
22
  table_config = ModelTableConfig.config
23
23
  table_config.migrate! unless table_config.compatible?
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aws-record-generator
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0.pre.1
4
+ version: 1.0.0.pre.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Amazon Web Services
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-06-13 00:00:00.000000000 Z
11
+ date: 2018-07-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aws-record
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '2'
19
+ version: '2.1'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '2'
26
+ version: '2.1'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rails
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -46,10 +46,26 @@ extensions: []
46
46
  extra_rdoc_files: []
47
47
  files:
48
48
  - lib/aws-record-generator.rb
49
+ - lib/generators/aws_record/active_model.rb
50
+ - lib/generators/aws_record/base.rb
51
+ - lib/generators/aws_record/erb/erb_generator.rb
52
+ - lib/generators/aws_record/erb/templates/_form.html.erb.tt
53
+ - lib/generators/aws_record/erb/templates/edit.html.erb.tt
54
+ - lib/generators/aws_record/erb/templates/index.html.erb.tt
55
+ - lib/generators/aws_record/erb/templates/new.html.erb.tt
56
+ - lib/generators/aws_record/erb/templates/show.html.erb.tt
49
57
  - lib/generators/aws_record/model/USAGE
50
58
  - lib/generators/aws_record/model/model_generator.rb
51
59
  - lib/generators/aws_record/model/templates/model.rb.tt
52
60
  - lib/generators/aws_record/model/templates/table_config.rb.tt
61
+ - lib/generators/aws_record/resource/USAGE
62
+ - lib/generators/aws_record/resource/resource_generator.rb
63
+ - lib/generators/aws_record/scaffold/USAGE
64
+ - lib/generators/aws_record/scaffold/scaffold_generator.rb
65
+ - lib/generators/aws_record/scaffold_controller/USAGE
66
+ - lib/generators/aws_record/scaffold_controller/scaffold_controller_generator.rb
67
+ - lib/generators/aws_record/scaffold_controller/templates/api_controller.rb.tt
68
+ - lib/generators/aws_record/scaffold_controller/templates/controller.rb.tt
53
69
  - lib/generators/generated_attribute.rb
54
70
  - lib/generators/secondary_index.rb
55
71
  - lib/generators/test_helper.rb