gatherable 1.2.0 → 2.0.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 +4 -4
- data/app/controllers/gatherable/application_controller.rb +23 -9
- data/config/routes.rb +20 -7
- data/lib/gatherable/configuration.rb +19 -1
- data/lib/gatherable/version.rb +1 -1
- data/lib/generators/gatherable/gatherable_generator.rb +5 -3
- data/lib/generators/gatherable/templates/gatherable.rb +6 -6
- data/spec/controllers/gatherable/application_controller_spec.rb +248 -134
- data/spec/dummy/config/initializers/gatherable.rb +0 -8
- data/spec/dummy/log/test.log +91257 -0
- data/spec/lib/gatherable_spec.rb +8 -13
- data/spec/lib/generators/gatherable_generator_spec.rb +5 -3
- data/spec/support/test_config.rb +0 -8
- metadata +2 -3
- data/lib/generators/gatherable/templates/application_controller.rb +0 -88
data/spec/lib/gatherable_spec.rb
CHANGED
@@ -6,7 +6,7 @@ describe Gatherable do
|
|
6
6
|
context 'data tables' do
|
7
7
|
context 'saving data tables' do
|
8
8
|
it 'saves data tables' do
|
9
|
-
expect(Gatherable.config.data_tables.count).to eql
|
9
|
+
expect(Gatherable.config.data_tables.count).to eql DataTable.all.keys.count
|
10
10
|
end
|
11
11
|
|
12
12
|
it 'saves data tables as data with correct name' do
|
@@ -67,12 +67,6 @@ describe Gatherable do
|
|
67
67
|
context 'routes' do
|
68
68
|
let(:routes) { Gatherable::Engine.routes.routes }
|
69
69
|
|
70
|
-
it 'prefixes route with global identifier' do
|
71
|
-
routes.each do |r|
|
72
|
-
expect(r.path.spec.to_s).to match(/^\/:session_id\//)
|
73
|
-
end
|
74
|
-
end
|
75
|
-
|
76
70
|
Gatherable.config.data_tables.map(&:name).map(&:to_s).each do |name|
|
77
71
|
it "creates a named path for #{name}" do
|
78
72
|
expect(routes.map(&:name)).to include name
|
@@ -83,12 +77,13 @@ describe Gatherable do
|
|
83
77
|
end
|
84
78
|
|
85
79
|
context 'generating routes' do
|
86
|
-
let(:
|
87
|
-
let(:
|
88
|
-
let(:
|
89
|
-
let(:
|
90
|
-
let(:
|
91
|
-
let(:
|
80
|
+
let(:route_prefix) { Gatherable.config.prefixed_resources.include?(name.to_sym) ? ':session_id/' : '' }
|
81
|
+
let(:create_route) { routes.find{ |r| r.verb == /^POST$/ && r.path.spec.to_s == "/#{route_prefix}#{name.pluralize}(.:format)" } }
|
82
|
+
let(:show_route) { routes.find{ |r| r.verb == /^GET$/ && r.path.spec.to_s == "/#{route_prefix}#{name.pluralize}/:#{name}_id(.:format)" } }
|
83
|
+
let(:index_route) { routes.find{ |r| r.verb == /^GET$/ && r.path.spec.to_s == "/#{route_prefix}#{name.pluralize}(.:format)" } }
|
84
|
+
let(:update_route) { routes.find{ |r| r.verb == /^PUT$/ && r.path.spec.to_s == "/#{route_prefix}#{name.pluralize}/:#{name}_id(.:format)" } }
|
85
|
+
let(:patch_route) { routes.find{ |r| r.verb == /^PATCH$/ && r.path.spec.to_s == "/#{route_prefix}#{name.pluralize}/:#{name}_id(.:format)" } }
|
86
|
+
let(:delete_route) { routes.find{ |r| r.verb == /^DELETE$/ && r.path.spec.to_s == "/#{route_prefix}#{name.pluralize}/:#{name}_id(.:format)" } }
|
92
87
|
|
93
88
|
specify 'for create' do
|
94
89
|
expect(create_route).to_not be_nil
|
@@ -10,7 +10,8 @@ describe GatherableGenerator, :type => :generator do
|
|
10
10
|
it 'creates initializer' do
|
11
11
|
generator = described_class.new(['initializer'])
|
12
12
|
allow(described_class).to receive(:new) { generator }
|
13
|
-
expect(generator).to receive(:copy_file).with('gatherable
|
13
|
+
expect(generator).to receive(:copy_file).with('lib/generators/gatherable/templates/gatherable.rb',
|
14
|
+
'config/initializers/gatherable.rb')
|
14
15
|
Rails::Generators.invoke('gatherable', ['initializer'])
|
15
16
|
end
|
16
17
|
|
@@ -85,7 +86,8 @@ end
|
|
85
86
|
context 'setup' do
|
86
87
|
it 'copies application controller' do
|
87
88
|
allow(Gatherable.config).to receive(:data_tables) { [] }
|
88
|
-
expect(generator).to receive(:copy_file).with('
|
89
|
+
expect(generator).to receive(:copy_file).with('app/controllers/gatherable/application_controller.rb',
|
90
|
+
'app/controllers/gatherable/application_controller.rb')
|
89
91
|
Rails::Generators.invoke('gatherable', ['controllers'])
|
90
92
|
end
|
91
93
|
end
|
@@ -127,7 +129,7 @@ var Price = {
|
|
127
129
|
});
|
128
130
|
}
|
129
131
|
}
|
130
|
-
|
132
|
+
content
|
131
133
|
end
|
132
134
|
let(:generator_target) { 'javascripts' }
|
133
135
|
it_behaves_like 'creating a file'
|
data/spec/support/test_config.rb
CHANGED
@@ -1,12 +1,4 @@
|
|
1
1
|
Gatherable.configure do |c|
|
2
2
|
c.global_identifier = :session_id
|
3
|
-
|
4
3
|
c.data_point :price, :decimal, allowed_controller_actions: %w[index show create update destroy]
|
5
|
-
|
6
|
-
c.data_table(
|
7
|
-
:requested_loan_amount,
|
8
|
-
{ requested_loan_amount: :decimal, total_cost: :decimal, monthly_repayment_amount: :decimal },
|
9
|
-
new_record_strategy: :update,
|
10
|
-
allowed_controller_actions: %w[index show create update destroy]
|
11
|
-
)
|
12
4
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: gatherable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Daniel Schepers
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-03-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -117,7 +117,6 @@ files:
|
|
117
117
|
- lib/gatherable/version.rb
|
118
118
|
- lib/generators/gatherable/USAGE
|
119
119
|
- lib/generators/gatherable/gatherable_generator.rb
|
120
|
-
- lib/generators/gatherable/templates/application_controller.rb
|
121
120
|
- lib/generators/gatherable/templates/gatherable.rb
|
122
121
|
- lib/tasks/db_tasks.rake
|
123
122
|
- lib/tasks/gatherable_tasks.rake
|
@@ -1,88 +0,0 @@
|
|
1
|
-
module Gatherable
|
2
|
-
class ApplicationController < ::ActionController::Base
|
3
|
-
before_action :authenticate, only: [:create]
|
4
|
-
|
5
|
-
def index
|
6
|
-
render :json => model_class.where(global_identifier => global_identifier), :status => :found
|
7
|
-
end
|
8
|
-
|
9
|
-
def show
|
10
|
-
render :json => model_instance, :status => :found
|
11
|
-
rescue ActiveRecord::RecordNotFound => e
|
12
|
-
render :json => { :errors => e.message}, :status => :not_found
|
13
|
-
end
|
14
|
-
|
15
|
-
def create
|
16
|
-
if data_table.new_record_strategy == :update
|
17
|
-
model = model_class.find_or_initialize_by(global_identifier => model_params[global_identifier])
|
18
|
-
model.update_attributes(model_params)
|
19
|
-
render :json => model, :status => :ok
|
20
|
-
else
|
21
|
-
render :json => model_class.create!(model_params), :status => :created
|
22
|
-
end
|
23
|
-
rescue StandardError => e
|
24
|
-
render :json => { :errors => e.message}, :status => :unprocessable_entity
|
25
|
-
end
|
26
|
-
|
27
|
-
def update
|
28
|
-
model_instance.update_attributes!(model_params)
|
29
|
-
render :json => model_instance, :status => :ok
|
30
|
-
rescue ActiveRecord::RecordNotFound => e
|
31
|
-
render :json => { :errors => e.message}, :status => :not_found
|
32
|
-
rescue StandardError => e
|
33
|
-
render :json => { :errors => e.message}, :status => :unprocessable_entity
|
34
|
-
end
|
35
|
-
|
36
|
-
def destroy
|
37
|
-
model_instance.delete
|
38
|
-
head :no_content
|
39
|
-
rescue ActiveRecord::RecordNotFound => e
|
40
|
-
render :json => { :errors => e.message}, :status => :not_found
|
41
|
-
end
|
42
|
-
|
43
|
-
private
|
44
|
-
|
45
|
-
def authenticate
|
46
|
-
return unless Gatherable.config.auth_method == :session
|
47
|
-
head :unauthorized unless params[global_identifier] == session[global_identifier]
|
48
|
-
end
|
49
|
-
|
50
|
-
def model_instance
|
51
|
-
model_class.find_by!(params.slice(model_id, global_identifier))
|
52
|
-
end
|
53
|
-
|
54
|
-
def model_class
|
55
|
-
Gatherable.const_get(unmodularized_model_name)
|
56
|
-
end
|
57
|
-
|
58
|
-
def model_name
|
59
|
-
self.class.to_s.chomp('Controller').singularize
|
60
|
-
end
|
61
|
-
|
62
|
-
def model_id
|
63
|
-
"#{model_name_as_var}_id"
|
64
|
-
end
|
65
|
-
|
66
|
-
def unmodularized_model_name
|
67
|
-
model_name.split('::').last
|
68
|
-
end
|
69
|
-
|
70
|
-
def model_name_as_var
|
71
|
-
unmodularized_model_name.underscore
|
72
|
-
end
|
73
|
-
|
74
|
-
def model_params
|
75
|
-
params.require(model_name_as_var).permit(
|
76
|
-
*model_class.column_names
|
77
|
-
).merge(global_identifier => params[global_identifier])
|
78
|
-
end
|
79
|
-
|
80
|
-
def global_identifier
|
81
|
-
Gatherable.config.global_identifier
|
82
|
-
end
|
83
|
-
|
84
|
-
def data_table
|
85
|
-
DataTable.find_by_name(model_name_as_var)
|
86
|
-
end
|
87
|
-
end
|
88
|
-
end
|