horse_power 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +3 -0
- data/Rakefile +34 -0
- data/app/assets/javascripts/horse_power/application.js +13 -0
- data/app/assets/stylesheets/horse_power/application.css +15 -0
- data/app/controllers/horse_power/application_controller.rb +4 -0
- data/app/helpers/horse_power/application_helper.rb +4 -0
- data/app/views/layouts/horse_power/application.html.erb +14 -0
- data/config/routes.rb +2 -0
- data/lib/generators/horse_power/app_environment/USAGE +8 -0
- data/lib/generators/horse_power/app_environment/app_environment_generator.rb +128 -0
- data/lib/generators/horse_power/authorization/USAGE +8 -0
- data/lib/generators/horse_power/authorization/authorization_generator.rb +20 -0
- data/lib/generators/horse_power/authorization/templates/auth_file.rb.erb +59 -0
- data/lib/generators/horse_power/controller/USAGE +8 -0
- data/lib/generators/horse_power/controller/controller_generator.rb +15 -0
- data/lib/generators/horse_power/controller/templates/controller_template.rb.erb +96 -0
- data/lib/generators/horse_power/make_admin/USAGE +8 -0
- data/lib/generators/horse_power/make_admin/make_admin_generator.rb +67 -0
- data/lib/generators/horse_power/make_admin/templates/dashboard.rb.erb +25 -0
- data/lib/generators/horse_power/make_admin/templates/defcon_admin_register.rb.erb +99 -0
- data/lib/generators/horse_power/model/USAGE +8 -0
- data/lib/generators/horse_power/model/model_generator.rb +21 -0
- data/lib/generators/horse_power/model/templates/model.rb.erb +5 -0
- data/lib/generators/horse_power/scaffold/USAGE +8 -0
- data/lib/generators/horse_power/scaffold/scaffold_generator.rb +54 -0
- data/lib/generators/horse_power/scaffold/templates/active_admin_register.rb.erb +6 -0
- data/lib/generators/horse_power/serializer/USAGE +8 -0
- data/lib/generators/horse_power/serializer/serializer_generator.rb +15 -0
- data/lib/generators/horse_power/serializer/templates/serializer.rb.erb +38 -0
- data/lib/generators/horse_power/setup/USAGE +8 -0
- data/lib/generators/horse_power/setup/setup_generator.rb +188 -0
- data/lib/generators/horse_power/setup/templates/active_admin_token_register.rb.erb +102 -0
- data/lib/generators/horse_power/setup/templates/app_index_controller.rb +6 -0
- data/lib/generators/horse_power/setup/templates/application_controller.rb +45 -0
- data/lib/generators/horse_power/setup/templates/hidden_rspec.rb +3 -0
- data/lib/generators/horse_power/setup/templates/json_helpers.rb +7 -0
- data/lib/generators/horse_power/setup/templates/rspec_factory_girl.rb +16 -0
- data/lib/generators/horse_power/setup/templates/views/app +0 -0
- data/lib/generators/horse_power/setup/templates/views/app_index +9 -0
- data/lib/generators/horse_power/tests/USAGE +8 -0
- data/lib/generators/horse_power/tests/templates/factory.rb.erb +12 -0
- data/lib/generators/horse_power/tests/templates/model.rb.erb +35 -0
- data/lib/generators/horse_power/tests/templates/request.rb.erb +136 -0
- data/lib/generators/horse_power/tests/templates/routing.rb.erb +46 -0
- data/lib/generators/horse_power/tests/tests_generator.rb +18 -0
- data/lib/generators/horse_power/user/USAGE +8 -0
- data/lib/generators/horse_power/user/templates/admin.rb.erb +20 -0
- data/lib/generators/horse_power/user/templates/authorization.rb.erb +97 -0
- data/lib/generators/horse_power/user/templates/controller.rb.erb +135 -0
- data/lib/generators/horse_power/user/templates/model.rb.erb +56 -0
- data/lib/generators/horse_power/user/templates/serializer.rb.erb +44 -0
- data/lib/generators/horse_power/user/templates/specs/factory.rb.erb +14 -0
- data/lib/generators/horse_power/user/templates/specs/model.rb.erb +61 -0
- data/lib/generators/horse_power/user/templates/specs/requests.rb.erb +220 -0
- data/lib/generators/horse_power/user/templates/specs/routing.rb.erb +58 -0
- data/lib/generators/horse_power/user/user_generator.rb +67 -0
- data/lib/horse_power/engine.rb +5 -0
- data/lib/horse_power/version.rb +3 -0
- data/lib/horse_power.rb +72 -0
- data/lib/tasks/horse_power_tasks.rake +4 -0
- data/test/dummy/README.rdoc +28 -0
- data/test/dummy/Rakefile +6 -0
- data/test/dummy/app/assets/javascripts/application.js +13 -0
- data/test/dummy/app/assets/stylesheets/application.css +15 -0
- data/test/dummy/app/controllers/application_controller.rb +5 -0
- data/test/dummy/app/helpers/application_helper.rb +2 -0
- data/test/dummy/app/views/layouts/application.html.erb +14 -0
- data/test/dummy/bin/bundle +3 -0
- data/test/dummy/bin/rails +4 -0
- data/test/dummy/bin/rake +4 -0
- data/test/dummy/config/application.rb +23 -0
- data/test/dummy/config/boot.rb +5 -0
- data/test/dummy/config/database.yml +25 -0
- data/test/dummy/config/environment.rb +5 -0
- data/test/dummy/config/environments/development.rb +37 -0
- data/test/dummy/config/environments/production.rb +78 -0
- data/test/dummy/config/environments/test.rb +39 -0
- data/test/dummy/config/initializers/assets.rb +8 -0
- data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/test/dummy/config/initializers/cookies_serializer.rb +3 -0
- data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/test/dummy/config/initializers/inflections.rb +16 -0
- data/test/dummy/config/initializers/mime_types.rb +4 -0
- data/test/dummy/config/initializers/session_store.rb +3 -0
- data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/test/dummy/config/locales/en.yml +23 -0
- data/test/dummy/config/routes.rb +4 -0
- data/test/dummy/config/secrets.yml +22 -0
- data/test/dummy/config.ru +4 -0
- data/test/dummy/public/404.html +67 -0
- data/test/dummy/public/422.html +67 -0
- data/test/dummy/public/500.html +66 -0
- data/test/dummy/public/favicon.ico +0 -0
- data/test/fresh/test_app/Gemfile +41 -0
- data/test/fresh/test_app/Gemfile.lock +127 -0
- data/test/fresh/test_app/README.rdoc +28 -0
- data/test/fresh/test_app/Rakefile +6 -0
- data/test/fresh/test_app/app/assets/javascripts/application.js +16 -0
- data/test/fresh/test_app/app/assets/stylesheets/application.css +15 -0
- data/test/fresh/test_app/app/controllers/application_controller.rb +5 -0
- data/test/fresh/test_app/app/helpers/application_helper.rb +2 -0
- data/test/fresh/test_app/app/views/layouts/application.html.erb +14 -0
- data/test/fresh/test_app/bin/bundle +3 -0
- data/test/fresh/test_app/bin/rails +8 -0
- data/test/fresh/test_app/bin/rake +8 -0
- data/test/fresh/test_app/bin/spring +18 -0
- data/test/fresh/test_app/config/application.rb +23 -0
- data/test/fresh/test_app/config/boot.rb +4 -0
- data/test/fresh/test_app/config/database.yml +25 -0
- data/test/fresh/test_app/config/environment.rb +5 -0
- data/test/fresh/test_app/config/environments/development.rb +37 -0
- data/test/fresh/test_app/config/environments/production.rb +78 -0
- data/test/fresh/test_app/config/environments/test.rb +39 -0
- data/test/fresh/test_app/config/initializers/assets.rb +8 -0
- data/test/fresh/test_app/config/initializers/backtrace_silencers.rb +7 -0
- data/test/fresh/test_app/config/initializers/cookies_serializer.rb +3 -0
- data/test/fresh/test_app/config/initializers/filter_parameter_logging.rb +4 -0
- data/test/fresh/test_app/config/initializers/inflections.rb +16 -0
- data/test/fresh/test_app/config/initializers/mime_types.rb +4 -0
- data/test/fresh/test_app/config/initializers/session_store.rb +3 -0
- data/test/fresh/test_app/config/initializers/wrap_parameters.rb +14 -0
- data/test/fresh/test_app/config/locales/en.yml +23 -0
- data/test/fresh/test_app/config/routes.rb +56 -0
- data/test/fresh/test_app/config/secrets.yml +22 -0
- data/test/fresh/test_app/config.ru +4 -0
- data/test/fresh/test_app/db/seeds.rb +7 -0
- data/test/fresh/test_app/public/404.html +67 -0
- data/test/fresh/test_app/public/422.html +67 -0
- data/test/fresh/test_app/public/500.html +66 -0
- data/test/fresh/test_app/public/favicon.ico +0 -0
- data/test/fresh/test_app/public/robots.txt +5 -0
- data/test/fresh/test_app/test/test_helper.rb +10 -0
- data/test/horse_power_test.rb +7 -0
- data/test/integration/navigation_test.rb +10 -0
- data/test/lib/generators/horse_power/app_environment_generator_test.rb +16 -0
- data/test/lib/generators/horse_power/authorization_generator_test.rb +16 -0
- data/test/lib/generators/horse_power/controller_generator_test.rb +16 -0
- data/test/lib/generators/horse_power/make_admin_generator_test.rb +16 -0
- data/test/lib/generators/horse_power/model_generator_test.rb +16 -0
- data/test/lib/generators/horse_power/scaffold_generator_test.rb +16 -0
- data/test/lib/generators/horse_power/serializer_generator_test.rb +16 -0
- data/test/lib/generators/horse_power/setup_generator_test.rb +16 -0
- data/test/lib/generators/horse_power/tests_generator_test.rb +16 -0
- data/test/lib/generators/horse_power/user_generator_test.rb +16 -0
- data/test/test_helper.rb +15 -0
- metadata +313 -0
@@ -0,0 +1,12 @@
|
|
1
|
+
FactoryGirl.define do
|
2
|
+
#sequence :name do |n|
|
3
|
+
# "name#{n}"
|
4
|
+
#end
|
5
|
+
factory :<%= HorsePower.get_singular(resource_name) %>_<%= api_version %>, class: <%= HorsePower.get_camel(resource_name) %> do
|
6
|
+
<% attributes.each do |pair| -%>
|
7
|
+
<%= HorsePower.get_column(pair) %> <%= HorsePower.default_values(HorsePower.get_type(pair)) %>
|
8
|
+
<% end -%>
|
9
|
+
#name
|
10
|
+
#attr "Default value"
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require "rails_helper"
|
2
|
+
|
3
|
+
<% attributes.each do |pair| %>
|
4
|
+
RSpec.describe <%= HorsePower.get_camel(resource_name) %>, '.<%= HorsePower.get_column(pair) %>', :type => :model do
|
5
|
+
describe "Attributes" do
|
6
|
+
it "does not save when attribute is nil" do
|
7
|
+
#<%= HorsePower.get_singular(resource_name) %> = FactoryGirl.build(:<%= HorsePower.get_singular(resource_name) %>_<%= api_version %>, :<%= HorsePower.get_column(pair) %> => nil)
|
8
|
+
#expect(<%= HorsePower.get_singular(resource_name) %>.save).to equal(false)
|
9
|
+
end
|
10
|
+
it "does not save when <%= pair %> is ..." do
|
11
|
+
#expect(<%= HorsePower.get_singular(resource_name) %>.save).to equal(false)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
<% end %>
|
16
|
+
RSpec.describe <%= HorsePower.get_camel(resource_name) %>, :type => :model do
|
17
|
+
describe "Dependencies" do
|
18
|
+
before(:example) do
|
19
|
+
@<%= HorsePower.get_singular(resource_name) %> = FactoryGirl.create(:<%= HorsePower.get_singular(resource_name) %>_<%= api_version %>)
|
20
|
+
end
|
21
|
+
it "deletes ... when deleted" do
|
22
|
+
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
RSpec.describe <%= HorsePower.get_camel(resource_name) %>, :type => :model do
|
27
|
+
describe "Callbacks" do
|
28
|
+
before(:example) do
|
29
|
+
@<%= HorsePower.get_singular(resource_name) %> = FactoryGirl.create(:<%= HorsePower.get_singular(resource_name) %>_<%= api_version %>)
|
30
|
+
end
|
31
|
+
it "creates a ..." do
|
32
|
+
#expect(object.posts.nil?).to equal(false)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,136 @@
|
|
1
|
+
require "rails_helper"
|
2
|
+
|
3
|
+
#-#-#-#-#REST#-#-#-#-#
|
4
|
+
RSpec.describe <%= HorsePower.get_camel(resource_name) %>, :type => :request do
|
5
|
+
describe "Index" do
|
6
|
+
before(:example) do
|
7
|
+
@user = FactoryGirl.create(:user_<%= api_version %>)
|
8
|
+
token = @user.tokens[0].auth_token
|
9
|
+
@header = {::Settings.token_header => token, "Email" => @user.email, ::Settings.main_api_header => ::Settings.main_api_key}
|
10
|
+
end
|
11
|
+
# get /api/<%= api_version %>/<%= HorsePower.get_plural(resource_name) %>
|
12
|
+
it "Gets all of the <%= HorsePower.get_singular(resource_name) %>s" do
|
13
|
+
FactoryGirl.create_list(:<%= HorsePower.get_singular(resource_name) %>_<%= api_version %>, 10)
|
14
|
+
get 'api/<%= api_version %>/<%= HorsePower.get_plural(resource_name) %>', nil, @header
|
15
|
+
expect(response.status).to eq(200) #ok
|
16
|
+
expect(<%= HorsePower.get_camel(resource_name) %>.count).to eq(10)
|
17
|
+
expect(json["<%= HorsePower.get_plural(resource_name) %>"].length).to eq(10)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
RSpec.describe <%= HorsePower.get_camel(resource_name) %>, :type => :request do
|
22
|
+
describe "Show" do
|
23
|
+
before(:example) do
|
24
|
+
@user = FactoryGirl.create(:user_<%= api_version %>)
|
25
|
+
token = @user.tokens[0].auth_token
|
26
|
+
@header = {::Settings.token_header => token, "Email" => @user.email, ::Settings.main_api_header => ::Settings.main_api_key}
|
27
|
+
end
|
28
|
+
# get /api/<%= api_version %>/<%= HorsePower.get_plural(resource_name) %>/1
|
29
|
+
it "Gets a <%= HorsePower.get_singular(resource_name) %> by id" do
|
30
|
+
<%= HorsePower.get_singular(resource_name) %> = FactoryGirl.create(:<%= HorsePower.get_singular(resource_name) %>_<%= api_version %>)
|
31
|
+
get "api/<%= api_version %>/<%= HorsePower.get_plural(resource_name) %>/#{<%= HorsePower.get_singular(resource_name) %>.id}", nil, @header
|
32
|
+
expect(response.status).to eq(200) #ok
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
RSpec.describe <%= HorsePower.get_camel(resource_name) %>, :type => :request do
|
37
|
+
describe "Create" do
|
38
|
+
before(:example) do
|
39
|
+
@user = FactoryGirl.create(:user_<%= api_version %>)
|
40
|
+
token = @user.tokens[0].auth_token
|
41
|
+
@header = {::Settings.token_header => token, "Email" => @user.email, ::Settings.main_api_header => ::Settings.main_api_key}
|
42
|
+
end
|
43
|
+
# post /api/<%= api_version %>/<%= HorsePower.get_plural(resource_name) %>
|
44
|
+
it "Creates <%= HorsePower.get_singular(resource_name) %>" do
|
45
|
+
attrs = FactoryGirl.attributes_for(:<%= HorsePower.get_singular(resource_name) %>_<%= api_version %>)
|
46
|
+
#attrs[:column] = "DefaultString"
|
47
|
+
hash = {"<%= HorsePower.get_singular(resource_name) %>" => attrs}
|
48
|
+
post 'api/<%= api_version %>/<%= HorsePower.get_plural(resource_name) %>', hash, @header
|
49
|
+
expect(response.status).to eq(200) #ok
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
RSpec.describe <%= HorsePower.get_camel(resource_name) %>, :type => :request do
|
54
|
+
describe "Update" do
|
55
|
+
before(:example) do
|
56
|
+
@user = FactoryGirl.create(:user_<%= api_version %>)
|
57
|
+
token = @user.tokens[0].auth_token
|
58
|
+
@header = {::Settings.token_header => token, "Email" => @user.email, ::Settings.main_api_header => ::Settings.main_api_key}
|
59
|
+
end
|
60
|
+
# patch/put /api/<%= api_version %>/<%= HorsePower.get_plural(resource_name) %>/1
|
61
|
+
it "Updates <%= HorsePower.get_singular(resource_name) %>" do
|
62
|
+
#Create the <%= HorsePower.get_singular(resource_name) %> through the api
|
63
|
+
attrs = FactoryGirl.attributes_for(:<%= HorsePower.get_singular(resource_name) %>_<%= api_version %>)
|
64
|
+
#attrs[:column] = "DefaultString"
|
65
|
+
<%= HorsePower.get_singular(resource_name) %> = FactoryGirl.create(:<%= HorsePower.get_singular(resource_name) %>_<%= api_version %>)
|
66
|
+
hash = {"<%= HorsePower.get_singular(resource_name) %>" => attrs}
|
67
|
+
put "api/<%= api_version %>/<%= HorsePower.get_plural(resource_name) %>/#{<%= HorsePower.get_singular(resource_name) %>.id}", hash, @header
|
68
|
+
expect(response.status).to eq(200) #ok
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
RSpec.describe <%= HorsePower.get_camel(resource_name) %>, :type => :request do
|
73
|
+
describe "Destroy" do
|
74
|
+
before(:example) do
|
75
|
+
@user = FactoryGirl.create(:user_<%= api_version %>)
|
76
|
+
token = @user.tokens[0].auth_token
|
77
|
+
@header = {::Settings.token_header => token, "Email" => @user.email, ::Settings.main_api_header => ::Settings.main_api_key}
|
78
|
+
end
|
79
|
+
# delete /api/<%= api_version %>/<%= HorsePower.get_plural(resource_name) %>/1
|
80
|
+
it "Deletes <%= HorsePower.get_singular(resource_name) %>" do
|
81
|
+
#Create the <%= HorsePower.get_singular(resource_name) %> through the api
|
82
|
+
attrs = FactoryGirl.attributes_for(:<%= HorsePower.get_singular(resource_name) %>_<%= api_version %>)
|
83
|
+
#attrs[:column] = "DefaultString"
|
84
|
+
<%= HorsePower.get_singular(resource_name) %> = FactoryGirl.create(:<%= HorsePower.get_singular(resource_name) %>_<%= api_version %>)
|
85
|
+
expect(<%= HorsePower.get_camel(resource_name) %>.count).to eq(1)
|
86
|
+
#Now delete the <%= HorsePower.get_singular(resource_name) %> through the api
|
87
|
+
delete "api/<%= api_version %>/<%= HorsePower.get_plural(resource_name) %>/#{<%= HorsePower.get_singular(resource_name) %>.id}", nil, @header
|
88
|
+
expect(json).to eq({})
|
89
|
+
expect(response.status).to eq(200) #ok
|
90
|
+
expect(<%= HorsePower.get_camel(resource_name) %>.count).to eq(0)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
#-#-#-#-#Collection Routes#-#-#-#-#
|
95
|
+
RSpec.describe <%= HorsePower.get_camel(resource_name) %>, :type => :request do
|
96
|
+
describe "Collection Routes" do
|
97
|
+
before(:example) do
|
98
|
+
@user = FactoryGirl.create(:user_<%= api_version %>)
|
99
|
+
token = @user.tokens[0].auth_token
|
100
|
+
@header = {::Settings.token_header => token, "Email" => @user.email, ::Settings.main_api_header => ::Settings.main_api_key}
|
101
|
+
end
|
102
|
+
# get /api/1/collection
|
103
|
+
it "checks response of a collection route" do
|
104
|
+
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
#-#-#-#-#Serialization#-#-#-#-#
|
109
|
+
RSpec.describe <%= HorsePower.get_camel(resource_name) %>, :type => :request do
|
110
|
+
describe "Serialization" do
|
111
|
+
before(:example) do
|
112
|
+
@user = FactoryGirl.create(:user_<%= api_version %>)
|
113
|
+
token = @user.tokens[0].auth_token
|
114
|
+
@header = {::Settings.token_header => token, "Email" => @user.email, ::Settings.main_api_header => ::Settings.main_api_key}
|
115
|
+
end
|
116
|
+
it "checks the index json sent back" do
|
117
|
+
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
#-#-#-#-#Errors#-#-#-#-#
|
122
|
+
RSpec.describe <%= HorsePower.get_camel(resource_name) %>, :type => :request do
|
123
|
+
describe "Errors" do
|
124
|
+
before(:example) do
|
125
|
+
@user = FactoryGirl.create(:user_<%= api_version %>)
|
126
|
+
token = @user.tokens[0].auth_token
|
127
|
+
@header = {::Settings.token_header => token, "Email" => @user.email, ::Settings.main_api_header => ::Settings.main_api_key}
|
128
|
+
end
|
129
|
+
# get /api/<%= api_version %>/<%= HorsePower.get_plural(resource_name) %>/1
|
130
|
+
it "checks for a 404" do
|
131
|
+
<%= HorsePower.get_singular(resource_name) %> = FactoryGirl.create(:<%= HorsePower.get_singular(resource_name) %>_<%= api_version %>)
|
132
|
+
get "api/<%= api_version %>/<%= HorsePower.get_plural(resource_name) %>/#{<%= HorsePower.get_singular(resource_name) %>.id + 1}", nil, @header
|
133
|
+
expect(response.status).to eq(404) #ok
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require "rails_helper"
|
2
|
+
|
3
|
+
#The standard rest routes for the <%= HorsePower.get_plural(resource_name) %> controller
|
4
|
+
RSpec.describe "<%= HorsePower.get_camel_plural(resource_name) %> controller rest routing", :type => :routing do
|
5
|
+
|
6
|
+
it "routes to index" do
|
7
|
+
expect(:get => "/api/<%= api_version %>/<%= HorsePower.get_plural(resource_name) %>").to route_to(
|
8
|
+
:controller => "api/v<%= api_version %>/<%= HorsePower.get_plural(resource_name) %>",
|
9
|
+
:action => "index"
|
10
|
+
)
|
11
|
+
end
|
12
|
+
it "routes to show" do
|
13
|
+
expect(:get => "/api/<%= api_version %>/<%= HorsePower.get_plural(resource_name) %>/1").to route_to(
|
14
|
+
:controller => "api/v<%= api_version %>/<%= HorsePower.get_plural(resource_name) %>",
|
15
|
+
:action => "show",
|
16
|
+
:id => "1"
|
17
|
+
)
|
18
|
+
end
|
19
|
+
it "routes to create" do
|
20
|
+
expect(:post => "/api/<%= api_version %>/<%= HorsePower.get_plural(resource_name) %>/").to route_to(
|
21
|
+
:controller => "api/v<%= api_version %>/<%= HorsePower.get_plural(resource_name) %>",
|
22
|
+
:action => "create"
|
23
|
+
)
|
24
|
+
end
|
25
|
+
it "routes to update" do
|
26
|
+
expect(:put => "/api/<%= api_version %>/<%= HorsePower.get_plural(resource_name) %>/1/").to route_to(
|
27
|
+
:controller => "api/v<%= api_version %>/<%= HorsePower.get_plural(resource_name) %>",
|
28
|
+
:action => "update",
|
29
|
+
:id => "1"
|
30
|
+
)
|
31
|
+
end
|
32
|
+
it "routes to update" do
|
33
|
+
expect(:patch => "/api/<%= api_version %>/<%= HorsePower.get_plural(resource_name) %>/1/").to route_to(
|
34
|
+
:controller => "api/v<%= api_version %>/<%= HorsePower.get_plural(resource_name) %>",
|
35
|
+
:action => "update",
|
36
|
+
:id => "1"
|
37
|
+
)
|
38
|
+
end
|
39
|
+
it "routes to delete" do
|
40
|
+
expect(:delete => "/api/<%= api_version %>/<%= HorsePower.get_plural(resource_name) %>/1/").to route_to(
|
41
|
+
:controller => "api/v<%= api_version %>/<%= HorsePower.get_plural(resource_name) %>",
|
42
|
+
:action => "destroy",
|
43
|
+
:id => "1"
|
44
|
+
)
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module HorsePower
|
2
|
+
class TestsGenerator < Rails::Generators::Base
|
3
|
+
source_root File.expand_path('../templates', __FILE__)
|
4
|
+
argument :resource_name, :type => :string
|
5
|
+
argument :api_version, :type => :string, :default => "1"
|
6
|
+
argument :attributes, type: :array, default: [], banner: "field:type field:type"
|
7
|
+
|
8
|
+
def sprint
|
9
|
+
template "model.rb.erb", "spec/api/v#{api_version}/models/#{HorsePower.get_singular(resource_name)}_spec.rb"
|
10
|
+
template "routing.rb.erb", "spec/api/v#{api_version}/routing/#{HorsePower.get_singular(resource_name)}_spec.rb"
|
11
|
+
template "factory.rb.erb", "spec/factories/#{HorsePower.get_singular(resource_name)}_#{api_version}_factory.rb"
|
12
|
+
template "request.rb.erb", "spec/api/v#{api_version}/requests/#{HorsePower.get_singular(resource_name)}_spec.rb"
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
ActiveAdmin.register User do
|
2
|
+
permit_params :username, :email, :password, :password_confirmation
|
3
|
+
|
4
|
+
config.per_page = 50
|
5
|
+
|
6
|
+
form do |f|
|
7
|
+
f.semantic_errors # shows errors on :base
|
8
|
+
f.inputs do
|
9
|
+
f.input :id
|
10
|
+
f.input :username
|
11
|
+
f.input :email
|
12
|
+
f.input :password
|
13
|
+
f.input :password_confirmation
|
14
|
+
f.input :updated_at
|
15
|
+
f.input :created_at
|
16
|
+
end
|
17
|
+
f.actions
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
@@ -0,0 +1,97 @@
|
|
1
|
+
module Authorization
|
2
|
+
module V1
|
3
|
+
module User
|
4
|
+
|
5
|
+
#Used in the controller
|
6
|
+
|
7
|
+
def self.index?(tokenUser)
|
8
|
+
return true
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.show?(targetUser,tokenUser)
|
12
|
+
return true
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.update?(targetUser,tokenUser)
|
16
|
+
if targetUser != tokenUser
|
17
|
+
#Can only update your own data
|
18
|
+
return false
|
19
|
+
else
|
20
|
+
return true
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.destroy?(targetUser,tokenUser)
|
25
|
+
if targetUser != tokenUser
|
26
|
+
#Can only destroy your own data
|
27
|
+
return false
|
28
|
+
else
|
29
|
+
return true
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.register?
|
34
|
+
return true
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.login?
|
38
|
+
return true
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.logout?(tokenUser)
|
42
|
+
return true
|
43
|
+
end
|
44
|
+
|
45
|
+
#Used in the serializer, current_user may be nil
|
46
|
+
|
47
|
+
def self.include_id?(current_user,user_object,options)
|
48
|
+
action = options[:url_options][:_recall][:action]
|
49
|
+
controller = options[:url_options][:_recall][:controller]
|
50
|
+
return true
|
51
|
+
end
|
52
|
+
|
53
|
+
def self.include_email?(current_user,user_object,options)
|
54
|
+
action = options[:url_options][:_recall][:action]
|
55
|
+
controller = options[:url_options][:_recall][:controller]
|
56
|
+
return true
|
57
|
+
end
|
58
|
+
|
59
|
+
def self.include_username?(current_user,user_object,options)
|
60
|
+
action = options[:url_options][:_recall][:action]
|
61
|
+
controller = options[:url_options][:_recall][:controller]
|
62
|
+
return true
|
63
|
+
end
|
64
|
+
|
65
|
+
def self.include_password_digest?(current_user,user_object,options)
|
66
|
+
action = options[:url_options][:_recall][:action]
|
67
|
+
controller = options[:url_options][:_recall][:controller]
|
68
|
+
return false
|
69
|
+
end
|
70
|
+
|
71
|
+
def self.include_created_at?(current_user,user_object,options)
|
72
|
+
action = options[:url_options][:_recall][:action]
|
73
|
+
controller = options[:url_options][:_recall][:controller]
|
74
|
+
return true
|
75
|
+
end
|
76
|
+
|
77
|
+
def self.include_updated_at?(current_user,user_object,options)
|
78
|
+
action = options[:url_options][:_recall][:action]
|
79
|
+
controller = options[:url_options][:_recall][:controller]
|
80
|
+
return true
|
81
|
+
end
|
82
|
+
|
83
|
+
|
84
|
+
def self.include_tokens?(current_user,user_object,options)
|
85
|
+
action = options[:url_options][:_recall][:action]
|
86
|
+
controller = options[:url_options][:_recall][:controller]
|
87
|
+
#if action == "index" && controller == "api/v1/users"
|
88
|
+
#return false
|
89
|
+
#end
|
90
|
+
return false
|
91
|
+
end
|
92
|
+
|
93
|
+
private
|
94
|
+
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
@@ -0,0 +1,135 @@
|
|
1
|
+
require_dependency "api/v1/application_controller"
|
2
|
+
require 'authorization'
|
3
|
+
|
4
|
+
class Api::V1::UsersController < Api::V1::ApplicationController
|
5
|
+
|
6
|
+
skip_before_filter :authenticate_user, :only => [:register, :login]
|
7
|
+
before_action :set_user, only: [:show, :update, :destroy]
|
8
|
+
before_action :index_authorize, only: [:index]
|
9
|
+
before_action :show_authorize, only: [:show]
|
10
|
+
before_action :update_authorize, only: [:update]
|
11
|
+
before_action :destroy_authorize, only: [:destroy]
|
12
|
+
before_action :register_authorize, only: [:register]
|
13
|
+
before_action :login_authorize, only: [:login]
|
14
|
+
before_action :logout_authorize, only: [:logout]
|
15
|
+
|
16
|
+
# GET /api/1/users
|
17
|
+
def index
|
18
|
+
@users = ::User.all
|
19
|
+
render json: @users, each_serializer: ::V1::UserSerializer
|
20
|
+
end
|
21
|
+
|
22
|
+
# GET /api/1/users/1
|
23
|
+
def show
|
24
|
+
render json: @user, serializer: ::V1::UserSerializer
|
25
|
+
end
|
26
|
+
|
27
|
+
# PATCH/PUT /api/1/users/1
|
28
|
+
def update
|
29
|
+
if @user.update(user_params)
|
30
|
+
render json: @user, serializer: ::V1::UserSerializer
|
31
|
+
else
|
32
|
+
render :json => {errors: @user.errors.full_messages}, status: :unprocessable_entity
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
# DELETE /api/1/users/1
|
37
|
+
def destroy
|
38
|
+
@user.destroy
|
39
|
+
render json: {}
|
40
|
+
end
|
41
|
+
|
42
|
+
# POST /api/1/users/register
|
43
|
+
def register
|
44
|
+
user = ::User.register(user_params)
|
45
|
+
if user.errors.full_messages == []
|
46
|
+
successful_login(user,user.tokens[0])
|
47
|
+
else
|
48
|
+
render :json => {errors: user.errors.full_messages}, status: :unprocessable_entity
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
# POST /api/1/users/login
|
53
|
+
def login
|
54
|
+
user = ::User.login(user_params)
|
55
|
+
if user
|
56
|
+
token = user.tokens.create
|
57
|
+
successful_login(user,token)
|
58
|
+
else
|
59
|
+
errors = "Email and/or Password is incorrect"
|
60
|
+
render :json => {errors: errors}, status: :unauthorized
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
# POST /api/1/users/logout
|
65
|
+
def logout
|
66
|
+
current_user.logout(current_token)
|
67
|
+
render json: {}
|
68
|
+
end
|
69
|
+
|
70
|
+
private
|
71
|
+
|
72
|
+
# Only allow a trusted parameter "white list" through.
|
73
|
+
|
74
|
+
def user_params
|
75
|
+
params.require(:user).permit(:username, :email, :password, :password_confirmation)
|
76
|
+
end
|
77
|
+
|
78
|
+
def set_user
|
79
|
+
@user = ::User.find_by_id(params[:id])
|
80
|
+
if @user.nil?
|
81
|
+
render :json => {errors: "User was not found"}, status: :not_found
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def successful_login(user,token)
|
86
|
+
::Arcadex::Create.set_token(token,20000,request,nil)
|
87
|
+
userHash = {id: user.id, username: user.username, email: user.email}
|
88
|
+
tokenHash = {auth_token: token.auth_token}
|
89
|
+
render :json => {user: userHash,token: tokenHash}
|
90
|
+
end
|
91
|
+
|
92
|
+
# Authorizations below here
|
93
|
+
|
94
|
+
def index_authorize
|
95
|
+
if !::Authorization::V1::User.index?(current_user)
|
96
|
+
render :json => {errors: "User is not authorized for this action"}, status: :forbidden
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
def show_authorize
|
101
|
+
if !::Authorization::V1::User.show?(@user,current_user)
|
102
|
+
render :json => {errors: "User is not authorized for this action"}, status: :forbidden
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
def update_authorize
|
107
|
+
if !::Authorization::V1::User.update?(@user,current_user)
|
108
|
+
render :json => {errors: "User is not authorized for this action"}, status: :forbidden
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
def destroy_authorize
|
113
|
+
if !::Authorization::V1::User.destroy?(@user,current_user)
|
114
|
+
render :json => {errors: "User is not authorized for this action"}, status: :forbidden
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
def register_authorize
|
119
|
+
if !::Authorization::V1::User.register?
|
120
|
+
render :json => {errors: "User is not authorized for this action"}, status: :forbidden
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
def login_authorize
|
125
|
+
if !::Authorization::V1::User.login?
|
126
|
+
render :json => {errors: "User is not authorized for this action"}, status: :forbidden
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
def logout_authorize
|
131
|
+
if !::Authorization::V1::User.logout?(current_user)
|
132
|
+
render :json => {errors: "User is not authorized for this action"}, status: :forbidden
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'type_cartographer'
|
2
|
+
|
3
|
+
class User < ActiveRecord::Base
|
4
|
+
|
5
|
+
before_save { self.email = email.downcase }
|
6
|
+
before_save { self.username = username.downcase }
|
7
|
+
before_validation :sanitize_attributes
|
8
|
+
after_create :setup_user
|
9
|
+
|
10
|
+
has_secure_password
|
11
|
+
|
12
|
+
has_many :tokens, :as => :imageable, :class_name => "::Arcadex::Token", dependent: :destroy
|
13
|
+
|
14
|
+
validates :username, presence: true, :uniqueness => { :case_sensitive => false }
|
15
|
+
#validates_format_of :username, :with => /\A[A-Za-z0-9\d]+\Z/i
|
16
|
+
validates :email, presence: true, :uniqueness => { :case_sensitive => false }
|
17
|
+
#Might need a regex for emails, or just rather confirm them
|
18
|
+
|
19
|
+
def sanitize_attributes
|
20
|
+
return true
|
21
|
+
end
|
22
|
+
|
23
|
+
def setup_user
|
24
|
+
create_token
|
25
|
+
return true
|
26
|
+
end
|
27
|
+
|
28
|
+
def create_token
|
29
|
+
self.tokens.create!
|
30
|
+
return true
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.register(user_params)
|
34
|
+
user = self.new(user_params)
|
35
|
+
user.save
|
36
|
+
return user
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.login(user_params)
|
40
|
+
email = user_params[:email]
|
41
|
+
if email
|
42
|
+
email = email.downcase
|
43
|
+
end
|
44
|
+
user = self.find_by(email: email)
|
45
|
+
if user && user.authenticate(user_params[:password])
|
46
|
+
return user
|
47
|
+
else
|
48
|
+
return nil
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def logout(token)
|
53
|
+
token.destroy
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'authorization'
|
2
|
+
|
3
|
+
class V1::UserSerializer < ActiveModel::Serializer
|
4
|
+
embed :ids, include: true
|
5
|
+
|
6
|
+
attributes :id
|
7
|
+
attributes :email
|
8
|
+
attributes :username
|
9
|
+
attributes :password_digest
|
10
|
+
attributes :created_at
|
11
|
+
attributes :updated_at
|
12
|
+
|
13
|
+
has_many :tokens
|
14
|
+
|
15
|
+
|
16
|
+
def include_id?
|
17
|
+
return ::Authorization::V1::User.include_id?(current_user,object,@options)
|
18
|
+
end
|
19
|
+
|
20
|
+
def include_email?
|
21
|
+
return ::Authorization::V1::User.include_email?(current_user,object,@options)
|
22
|
+
end
|
23
|
+
|
24
|
+
def include_username?
|
25
|
+
return ::Authorization::V1::User.include_username?(current_user,object,@options)
|
26
|
+
end
|
27
|
+
|
28
|
+
def include_password_digest?
|
29
|
+
return ::Authorization::V1::User.include_password_digest?(current_user,object,@options)
|
30
|
+
end
|
31
|
+
|
32
|
+
def include_created_at?
|
33
|
+
return ::Authorization::V1::User.include_created_at?(current_user,object,@options)
|
34
|
+
end
|
35
|
+
|
36
|
+
def include_updated_at?
|
37
|
+
return ::Authorization::V1::User.include_updated_at?(current_user,object,@options)
|
38
|
+
end
|
39
|
+
|
40
|
+
def include_associations!
|
41
|
+
include! :tokens if ::Authorization::V1::User.include_tokens?(current_user,object,@options)
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
FactoryGirl.define do
|
2
|
+
sequence :username do |n|
|
3
|
+
"username#{n}"
|
4
|
+
end
|
5
|
+
sequence :email do |n|
|
6
|
+
"username#{n}@email.com"
|
7
|
+
end
|
8
|
+
factory :user_1, class: User do
|
9
|
+
username
|
10
|
+
email
|
11
|
+
password "password123"
|
12
|
+
password_confirmation "password123"
|
13
|
+
end
|
14
|
+
end
|