rails_base_api_scaffold 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c38e6bc51f540f22efd5882a5fb4a95554da5413
4
+ data.tar.gz: 29e0fd7eb9891501824db16ae222ac0b5ab8ff1b
5
+ SHA512:
6
+ metadata.gz: 4f2ee067976ae6d80a6a751da5a1a31fc433807356c9ce918e23960f61b34d35749cfe2de7423de888b5d034d429458d98d147a185f1857a2d36a1170b098a2b
7
+ data.tar.gz: 878346c6ff445be1f3ee8abe7450e6cac5e587e8c9ad273eae1bf1946540f3ea1240e6185c09f1aae32b7c5ba2a79f61b31c2bba279b17854afd4cc1efec5332
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.1
5
+ before_install: gem install bundler -v 1.15.3
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in rails_base_api_scaffold.gemspec
6
+ gemspec
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # RailsBaseApiScaffold
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/rails_base_api_scaffold`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'rails_base_api_scaffold'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install rails_base_api_scaffold
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/rails_base_api_scaffold.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "rails_base_api_scaffold"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,37 @@
1
+ require 'generators/rails_base_api_scaffold/generator_helpers'
2
+
3
+ module RailsBaseApiScaffold
4
+ module Generators
5
+ # Custom scaffolding generator
6
+ class ControllerGenerator < Rails::Generators::NamedBase
7
+ include Rails::Generators::ResourceHelpers
8
+ include RailsBaseApiScaffold::Generators::GeneratorHelpers
9
+
10
+ desc "Generates controller, controller_spec and views for the model with the given NAME."
11
+
12
+ source_root File.expand_path('../templates', __FILE__)
13
+
14
+ def copy_templates
15
+ template "controller.rb", File.join("app/controllers/api/v1", "#{controller_file_name}_controller.rb")
16
+ template "serializer.rb", File.join("app/serializers", "#{file_name}_serializer.rb")
17
+ template "spec.rb", File.join("spec/requests/api/v1", "#{controller_file_name}_controller_spec.rb")
18
+ end
19
+
20
+ def add_routes
21
+ # Include tabs and line break for proper formatting
22
+ routes_string = " resources :#{plural_name}\n"
23
+ # Inject into file following the api and v1 namespaces
24
+ inject_into_file 'config/routes.rb', after: " namespace :api do\n namespace :v1 do\n" do
25
+ routes_string
26
+ end
27
+ end
28
+
29
+ def add_scoped_search
30
+ scoped_search_string = " scoped_search on: [ :id, :created_at, :updated_at ], only_explicit: true\n"
31
+ inject_into_file "app/models/#{file_name}.rb", after: "class #{class_name} < ActiveRecord::Base\n" do
32
+ scoped_search_string
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,42 @@
1
+ module RailsBaseApiScaffold
2
+ module Generators
3
+ # Some helpers for generating scaffolding
4
+ module GeneratorHelpers
5
+ attr_accessor :options, :attributes
6
+
7
+ private
8
+
9
+ def model_columns_for_attributes
10
+ class_name.constantize.columns.reject do |column|
11
+ column.name.to_s =~ /^(id|user_id|created_at|updated_at)$/
12
+ end
13
+ end
14
+
15
+ def editable_attributes
16
+ attributes ||= model_columns_for_attributes.map do |column|
17
+ Rails::Generators::GeneratedAttribute.new(column.name.to_s, column.type.to_s)
18
+ end
19
+ end
20
+
21
+ def mock_editable_attribute_key
22
+ attribute = editable_attributes.first
23
+ attribute.name
24
+ end
25
+
26
+ def mock_editable_attribute_value
27
+ attribute = editable_attributes.first
28
+ attribute.type == 'string' ? '"Test"' :
29
+ attribute.type == 'text' ? '"Test"' :
30
+ attribute.type == 'integer' ? 1 :
31
+ attribute.type == 'float' ? 1.25 :
32
+ attribute.type == 'decimal' ? 1.50 :
33
+ attribute.type == 'datetime' ? '2017-12-27 00:00:00' :
34
+ attribute.type == 'time' ? '00:00:00' :
35
+ attribute.type == 'date' ? '2017-12-27' :
36
+ attribute.type == 'binary' ? '"Test"' :
37
+ attribute.type == 'boolean' ? true :
38
+ nil
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,90 @@
1
+ module Api::V1
2
+ class <%= controller_class_name %>Controller < ApiController
3
+ respond_to :json
4
+
5
+ check_authorization
6
+
7
+ before_action :doorkeeper_authorize!
8
+
9
+ skip_before_filter :verify_authenticity_token
10
+
11
+ load_and_authorize_resource
12
+
13
+ helper_method :sort_order, :sort_column, :sort_direction
14
+
15
+ # GET /<%= plural_name %>/
16
+ def index
17
+
18
+ page_size = params[:page_size] ? params[:page_size].to_i : 20
19
+
20
+ raise RangeError.new("invalid page_size: #{page_size}") if page_size < 1
21
+
22
+ @<%= plural_name %> = <%= class_name %>.search_for(params[:search], order: sort_order).paginate(page: params[:page], per_page: page_size)
23
+
24
+ page_counts = @<%= plural_name %>.total_entries.divmod(page_size)
25
+ page_count = page_counts[0] + ((page_counts[1] > 0) ? 1 : 0)
26
+
27
+ render json: @<%= plural_name %>,
28
+ root: '<%= plural_name %>',
29
+ meta: { page_count: page_count, page_size: page_size }, adapter: :json
30
+
31
+ end
32
+
33
+ # GET /<%= plural_name %>/1
34
+ def show
35
+ render json: @<%= singular_name %>, serializer: <%= class_name %>Serializer, root: false
36
+ end
37
+
38
+ # POST /<%= plural_name %>/1
39
+ def create
40
+ @<%= singular_name %> = <%= class_name %>.create(<%= singular_name %>_params)
41
+
42
+ if @<%= singular_name %>
43
+ render json: @<%= singular_name %>, serializer: <%= class_name %>Serializer, status: :created
44
+ else
45
+ render json: @<%= singular_name %>.errors, status: :unprocessable_entity
46
+ end
47
+ end
48
+
49
+ # PUT /<%= plural_name %>/1
50
+ def update
51
+ if <%= singular_name %>.update(<%= singular_name %>_params)
52
+ render json: <%= singular_name %>, serializer: <%= class_name %>Serializer, status: :ok
53
+ else
54
+ render json: <%= singular_name %>.errors, status: :unprocessable_entity
55
+ end
56
+ end
57
+
58
+ # DELETE /<%= plural_name %>/1
59
+ def destroy
60
+ <%= singular_name %>.destroy
61
+ render json: { message: '<%= human_name %> deleted' }
62
+ end
63
+
64
+ private
65
+
66
+ # Use callbacks to share common setup or constraints between actions.
67
+ def <%= singular_name %>
68
+ @<%= singular_name %> ||= <%= class_name %>.find(params[:id])
69
+ end
70
+
71
+ # Add index sorting by default.
72
+ def sort_order
73
+ sort_column + ' ' + sort_direction
74
+ end
75
+
76
+ def sort_column
77
+ <%= class_name %>.column_names.include?(params[:sort]) ? params[:sort] : 'created_at'
78
+ end
79
+
80
+ def sort_direction
81
+ %w[asc desc].include?(params[:direction]) ? params[:direction] : 'desc'
82
+ end
83
+
84
+ # Only allow a trusted parameter "white list" through.
85
+ def <%= singular_name %>_params
86
+ params.require(:<%= singular_name %>).permit([<%= editable_attributes.map { |a| ":#{a.name}" }.join(', ') %>])
87
+ end
88
+ end
89
+ end
90
+
@@ -0,0 +1,3 @@
1
+ class <%= class_name %>Serializer < ActiveModel::Serializer
2
+ attributes :id, <%= editable_attributes.map { |a| ":#{a.name}" }.join(', ') %>
3
+ end
@@ -0,0 +1,120 @@
1
+ require 'rails_helper'
2
+
3
+ RSpec.describe '<%= controller_class_name %>Controller', :type => :request do
4
+ describe 'GET /<%= plural_name %>' do
5
+
6
+ context 'user is not created' do
7
+ it 'returns a 401 if user is not authenticated' do
8
+ response = oauth_get api_v1_<%= plural_name %>_path
9
+ expect(response.status).to eq 401
10
+ end
11
+ end
12
+
13
+ context 'user created' do
14
+ before(:each) do
15
+ @user = create(:admin)
16
+ prepare_user_oauth @user
17
+ end
18
+
19
+ it 'returns a list of <%= plural_name %>' do
20
+ <%= singular_name %> = create(:<%= singular_name %>)
21
+
22
+ response = oauth_get api_v1_<%= plural_name %>_path, { page_size: 10 }
23
+ expect(response.status).to eq 200
24
+
25
+ json_response = JSON.parse response.body
26
+
27
+ expect(json_response['<%= plural_name %>']).to_not be_empty
28
+ expect(json_response['<%= plural_name %>'].length).to eq 1
29
+ end
30
+ end
31
+ end
32
+
33
+ describe 'POST /<%= plural_name %>' do
34
+ context 'user is not created' do
35
+ it 'returns a 401 if user is not authenticated' do
36
+ posted_data = {
37
+ <%= mock_editable_attribute_key %>: <%= mock_editable_attribute_value %>
38
+ }
39
+ response = oauth_post api_v1_<%= plural_name %>_path, { <%= singular_name %>: posted_data }
40
+ expect(response.status).to eq 401
41
+ end
42
+ end
43
+
44
+ context 'user created' do
45
+ it 'creates a <%= singular_name %> for authenticated user' do
46
+ user = create(:admin)
47
+ prepare_user_oauth user
48
+
49
+ posted_data = {
50
+ <%= mock_editable_attribute_key %>: <%= mock_editable_attribute_value %>
51
+ }
52
+
53
+ response = oauth_post api_v1_<%= plural_name %>_path, { <%= singular_name %>: posted_data }
54
+ expect(response.status).to eq 201
55
+
56
+ json_response = JSON.parse response.body
57
+
58
+ expect(json_response['id']).to be_a Numeric
59
+ expect(json_response['<%= mock_editable_attribute_key %>']).to eq <%= mock_editable_attribute_value %>
60
+ end
61
+ end
62
+ end
63
+
64
+ describe 'PUT /<%= plural_name %>/:id' do
65
+ context 'user is not created' do
66
+ it 'returns a 401 if user is not authenticated' do
67
+ <%= singular_name %> = create(:<%= singular_name %>)
68
+ response = oauth_put api_v1_<%= singular_name %>_path(<%= singular_name %>)
69
+ expect(response.status).to eq 401
70
+ end
71
+ end
72
+
73
+ context 'user created' do
74
+ it 'updates a <%= singular_name %> for authenticated user' do
75
+ user = create(:admin)
76
+ <%= singular_name %> = create(:<%= singular_name %>)
77
+
78
+ prepare_user_oauth user
79
+
80
+ posted_data = {
81
+ <%= mock_editable_attribute_key %>: <%= mock_editable_attribute_value %>
82
+ }
83
+
84
+ response = oauth_put api_v1_<%= singular_name %>_path(<%= singular_name %>), { <%= singular_name %>: posted_data }
85
+ expect(response.status).to eq 200
86
+
87
+ json_response = JSON.parse response.body
88
+
89
+ expect(json_response['id']).to eq <%= singular_name %>.id
90
+ expect(json_response['<%= mock_editable_attribute_key %>']).to eq <%= mock_editable_attribute_value %>
91
+ end
92
+ end
93
+ end
94
+
95
+ describe 'DELETE /<%= plural_name %>/:id' do
96
+ context 'user is not created' do
97
+ it 'returns a 401 if user is not authenticated' do
98
+ <%= singular_name %> = create(:<%= singular_name %>)
99
+ response = oauth_del api_v1_<%= singular_name %>_path(<%= singular_name %>)
100
+ expect(response.status).to eq 401
101
+ end
102
+ end
103
+
104
+ context 'user created' do
105
+ it 'updates a <%= singular_name %> for authenticated user' do
106
+ user = create(:admin)
107
+ <%= singular_name %> = create(:<%= singular_name %>)
108
+
109
+ prepare_user_oauth user
110
+
111
+ response = oauth_del api_v1_<%= singular_name %>_path(<%= singular_name %>)
112
+ expect(response.status).to eq 200
113
+
114
+ json_response = JSON.parse response.body
115
+
116
+ expect(json_response['message']).to eq '<%= human_name %> deleted'
117
+ end
118
+ end
119
+ end
120
+ end
@@ -0,0 +1,3 @@
1
+ module RailsBaseApiScaffold
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,5 @@
1
+ require "rails_base_api_scaffold/version"
2
+
3
+ module RailsBaseApiScaffold
4
+ # Your code goes here...
5
+ end
@@ -0,0 +1,34 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "rails_base_api_scaffold/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rails_base_api_scaffold"
8
+ spec.version = RailsBaseApiScaffold::VERSION
9
+ spec.authors = ["Christian McCormick"]
10
+ spec.email = ["cmccormick421@gmail.com"]
11
+
12
+ spec.summary = "Scaffold for the E7Systems Rails Base API"
13
+ spec.homepage = "https://github.com/christianmccormick/rails_base_api_scaffold"
14
+
15
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
16
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
17
+ if spec.respond_to?(:metadata)
18
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
19
+ else
20
+ raise "RubyGems 2.0 or newer is required to protect against " \
21
+ "public gem pushes."
22
+ end
23
+
24
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
25
+ f.match(%r{^(test|spec|features)/})
26
+ end
27
+ spec.bindir = "exe"
28
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ["lib"]
30
+
31
+ spec.add_development_dependency "bundler", "~> 1.15"
32
+ spec.add_development_dependency "rake", "~> 10.0"
33
+ spec.add_development_dependency "rspec", "~> 3.0"
34
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails_base_api_scaffold
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Christian McCormick
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-12-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.15'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.15'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description:
56
+ email:
57
+ - cmccormick421@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - .rspec
64
+ - .travis.yml
65
+ - Gemfile
66
+ - README.md
67
+ - Rakefile
68
+ - bin/console
69
+ - bin/setup
70
+ - lib/generators/rails_base_api_scaffold/controller_generator.rb
71
+ - lib/generators/rails_base_api_scaffold/generator_helpers.rb
72
+ - lib/generators/rails_base_api_scaffold/templates/controller.rb
73
+ - lib/generators/rails_base_api_scaffold/templates/serializer.rb
74
+ - lib/generators/rails_base_api_scaffold/templates/spec.rb
75
+ - lib/rails_base_api_scaffold.rb
76
+ - lib/rails_base_api_scaffold/version.rb
77
+ - rails_base_api_scaffold.gemspec
78
+ homepage: https://github.com/christianmccormick/rails_base_api_scaffold
79
+ licenses: []
80
+ metadata:
81
+ allowed_push_host: https://rubygems.org
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - '>='
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 2.0.14.1
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: Scaffold for the E7Systems Rails Base API
102
+ test_files: []