standardapi 1.0.13 → 1.0.14

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4f276df6b8c02e60277971a4a7c0b4119805a813
4
- data.tar.gz: f1a04f95d568360aef96f66c5544cd257c210f7c
3
+ metadata.gz: bae8ab8cb0e9504d2a04105b232912be7d2e800d
4
+ data.tar.gz: 0685d8c1c8cde8f423bc29b20443226297090814
5
5
  SHA512:
6
- metadata.gz: 55a1400893f87c82b670ca5b0f523249dfdf719cb0c4521a286e879fdc5b352a95b9aae7e5f91b947562647863c58f7eae1a919aea203fb13a20d6017081559a
7
- data.tar.gz: 220e40e163998406f669414adeb90d83ed0a957fac003e82532a37f94e527a9cedcb2bd9954ffe4883b6549331ba7843db4807690ff51ba48dc52ca8ea4cd446
6
+ metadata.gz: 3c677a3aea24f4d7f1409fd149571954a705bdc50d163131be72fe27602c85d9554498b194ad0c4635fdeb55d1b9fb91c87d96edc166630eb608f808c8827053
7
+ data.tar.gz: 0bf6e349cd22b92126e6d0fc15057299b36ad296349618496755bce7d1c1ff09a2f8269eba65d93781ecff74d752556c317b2b18f294706e04fd936f19ecf471
@@ -0,0 +1,146 @@
1
+ module StandardAPI
2
+ module Controller
3
+
4
+ def self.included(klass)
5
+ klass.hide_action :current_mask
6
+ klass.helper_method :includes, :orders, :model
7
+ klass.append_view_path(File.join(File.dirname(__FILE__), 'views'))
8
+ klass.extend(ClassMethods)
9
+ end
10
+
11
+ def ping
12
+ render :text => 'pong'
13
+ end
14
+
15
+ def tables
16
+ controllers = Dir[Rails.root.join('app/controllers/*_controller.rb')].map{ |path| path.match(/(\w+)_controller.rb/)[1].camelize+"Controller" }.map(&:safe_constantize)
17
+ controllers.select! { |c| c.ancestors.include?(self.class) && c != self.class }
18
+ controllers.map!(&:model).compact!.map!(&:table_name)
19
+
20
+ render json: controllers
21
+ end
22
+
23
+ def index
24
+ @records = resources.limit(params[:limit]).offset(params[:offset]).sort(orders)
25
+ instance_variable_set("@#{model.model_name.plural}", @records)
26
+ end
27
+
28
+ def calculate
29
+ @calculations = resources.reorder(nil).pluck(*calculate_selects).map do |c|
30
+ if c.is_a?(Array)
31
+ c.map { |v| v.is_a?(BigDecimal) ? v.to_f : v }
32
+ else
33
+ c.is_a?(BigDecimal) ? c.to_f : c
34
+ end
35
+ end
36
+ render json: @calculations
37
+ end
38
+
39
+ def show
40
+ @record = resources.find(params[:id])
41
+ instance_variable_set("@#{model.model_name.singular}", @record)
42
+ end
43
+
44
+ def create
45
+ @record = model.new(model_params)
46
+ instance_variable_set("@#{model.model_name.singular}", @record)
47
+ render :show, status: @record.save ? :created : :bad_request
48
+ end
49
+
50
+ def update
51
+ @record = resources.find(params[:id])
52
+ instance_variable_set("@#{model.model_name.singular}", @record)
53
+ render :show, status: @record.update_attributes(model_params) ? :ok : :bad_request
54
+ end
55
+
56
+ def destroy
57
+ resources.find(params[:id]).destroy!
58
+ render nothing: true, status: :no_content
59
+ end
60
+
61
+ # Override if you want to support masking
62
+ def current_mask
63
+ @current_mask ||= {}
64
+ end
65
+
66
+ module ClassMethods
67
+
68
+ def model
69
+ return @model if defined?(@model)
70
+ @model = name.sub(/Controller\z/, '').singularize.camelize.safe_constantize
71
+ end
72
+
73
+ end
74
+
75
+ private
76
+
77
+ def model
78
+ self.class.model
79
+ end
80
+
81
+ def model_params
82
+ params.require(model.model_name.singular).permit(self.send("#{model.model_name.singular}_params"))
83
+ end
84
+
85
+ def model_includes
86
+ self.send "#{model.model_name.singular}_includes"
87
+ end
88
+
89
+ def model_orders
90
+ self.send "#{model.model_name.singular}_orders"
91
+ end
92
+
93
+ def excludes_for(klass)
94
+ if defined?(ApplicationHelper) && ApplicationHelper.instance_methods.include?(:excludes)
95
+ excludes = Class.new.send(:include, ApplicationHelper).new.excludes.with_indifferent_access
96
+ excludes.try(:[], klass.model_name.singular) || []
97
+ else
98
+ []
99
+ end
100
+ end
101
+
102
+ def model_excludes
103
+ excludes_for(model)
104
+ end
105
+
106
+ def resources
107
+ model.filter(params[:where]).where(current_mask[model.table_name])
108
+ end
109
+
110
+ def includes
111
+ @includes ||= StandardAPI::Includes.normalize(params[:include])
112
+ end
113
+
114
+ def orders
115
+ @orders ||= StandardAPI::Orders.sanitize(params[:order], model_orders)
116
+ end
117
+
118
+ def excludes
119
+ @excludes ||= model_excludes
120
+ end
121
+
122
+ # Used in #calculate
123
+ # [{ count: :id }]
124
+ # [{ count: '*' }]
125
+ # [{ count: '*', maximum: :id, minimum: :id }]
126
+ # [{ count: '*' }, { maximum: :id }, { minimum: :id }]
127
+ # TODO: Sanitize (normalize_select_params(params[:select], model))
128
+ def calculate_selects
129
+ return @selects if defined?(@selects)
130
+
131
+ functions = ['minimum', 'maximum', 'average', 'sum', 'count']
132
+ @selects = []
133
+ Array(params[:select]).each do |select|
134
+ select.each do |func, column|
135
+ column = column == '*' ? Arel.star : column.to_sym
136
+ if functions.include?(func.to_s.downcase)
137
+ @selects << (model.arel_table[column].send(func).to_sql)
138
+ end
139
+ end
140
+ end
141
+
142
+ @selects
143
+ end
144
+
145
+ end
146
+ end
@@ -0,0 +1,13 @@
1
+ module StandardAPI
2
+ module Helpers
3
+
4
+ def model_partial(record)
5
+ if lookup_context.exists?(record.model_name.element, controller_name)
6
+ [record.model_name.plural, record.model_name.element].join('/')
7
+ else
8
+ 'application/record'
9
+ end
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,11 @@
1
+ module StandardAPI
2
+ class Railtie < ::Rails::Railtie
3
+
4
+ initializer 'standardapi' do
5
+ ActiveSupport.on_load(:action_view) do
6
+ ::ActionView::Base.send :include, StandardAPI::Helpers
7
+ end
8
+ end
9
+
10
+ end
11
+ end
@@ -10,11 +10,7 @@ includes.each do |inc, subinc|
10
10
  json.set! inc do
11
11
  collection = [:has_many, :has_and_belongs_to_many].include?(association.macro)
12
12
 
13
- partial = if lookup_context.exists?(association.klass.model_name.element, controller_name)
14
- association.klass.model_name.element
15
- else
16
- 'record'
17
- end
13
+ partial = model_partial(association.klass)
18
14
 
19
15
  if collection
20
16
  json.array! record.send(inc), partial: partial, as: :record, locals: { includes: subinc }
@@ -1,9 +1,3 @@
1
1
  records = @records if !records
2
2
 
3
- # partial = if lookup_context.exists?(association.klass.model_name.element, controller_name)
4
- # association.klass.model_name.element
5
- # else
6
- # 'record'
7
- # end
8
-
9
- json.array! records, partial: 'application/record', as: :record, includes: includes
3
+ json.array! records, partial: model_partial(model), as: :record, includes: includes
@@ -1,9 +1,3 @@
1
1
  record = @record if !record
2
2
 
3
- # partial = if lookup_context.exists?(association.klass.model_name.element, controller_name)
4
- # association.klass.model_name.element
5
- # else
6
- # 'record'
7
- # end
8
-
9
- json.partial! 'application/record', record: record, includes: includes
3
+ json.partial! model_partial(model), record: record, includes: includes
data/lib/standard_api.rb CHANGED
@@ -8,154 +8,12 @@ require 'active_record/filter'
8
8
  require 'active_record/sort'
9
9
  require 'active_support/core_ext/hash/indifferent_access'
10
10
 
11
- if !ActionView::Template.registered_template_handler(:jbuilder)
12
- ActionView::Template.register_template_handler :jbuilder, JbuilderHandler
11
+ module StandardAPI
13
12
  end
14
13
 
15
14
  require 'standard_api/orders'
16
15
  require 'standard_api/includes'
16
+ require 'standard_api/controller'
17
+ require 'standard_api/helpers'
18
+ require 'standard_api/railtie'
17
19
 
18
- module StandardAPI
19
-
20
- def self.included(klass)
21
- klass.hide_action :current_mask
22
- klass.helper_method :includes, :orders, :model
23
- klass.append_view_path(File.join(File.dirname(__FILE__), 'standard_api', 'views'))
24
- klass.extend(ClassMethods)
25
- end
26
-
27
- def ping
28
- render :text => 'pong'
29
- end
30
-
31
- def tables
32
- controllers = Dir[Rails.root.join('app/controllers/*_controller.rb')].map{ |path| path.match(/(\w+)_controller.rb/)[1].camelize+"Controller" }.map(&:safe_constantize)
33
- controllers.select! { |c| c.ancestors.include?(self.class) && c != self.class }
34
- controllers.map!(&:model).compact!.map!(&:table_name)
35
-
36
- render json: controllers
37
- end
38
-
39
- def index
40
- @records = resources.limit(params[:limit]).offset(params[:offset]).sort(orders)
41
- instance_variable_set("@#{model.model_name.plural}", @records)
42
- end
43
-
44
- def calculate
45
- @calculations = resources.reorder(nil).pluck(*calculate_selects).map do |c|
46
- if c.is_a?(Array)
47
- c.map { |v| v.is_a?(BigDecimal) ? v.to_f : v }
48
- else
49
- c.is_a?(BigDecimal) ? c.to_f : c
50
- end
51
- end
52
- render json: @calculations
53
- end
54
-
55
- def show
56
- @record = resources.find(params[:id])
57
- instance_variable_set("@#{model.model_name.singular}", @record)
58
- end
59
-
60
- def create
61
- @record = model.new(model_params)
62
- instance_variable_set("@#{model.model_name.singular}", @record)
63
- render :show, status: @record.save ? :created : :bad_request
64
- end
65
-
66
- def update
67
- @record = resources.find(params[:id])
68
- instance_variable_set("@#{model.model_name.singular}", @record)
69
- render :show, status: @record.update_attributes(model_params) ? :ok : :bad_request
70
- end
71
-
72
- def destroy
73
- resources.find(params[:id]).destroy!
74
- render nothing: true, status: :no_content
75
- end
76
-
77
- # Override if you want to support masking
78
- def current_mask
79
- @current_mask ||= {}
80
- end
81
-
82
- module ClassMethods
83
-
84
- def model
85
- return @model if defined?(@model)
86
- @model = name.sub(/Controller\z/, '').singularize.camelize.safe_constantize
87
- end
88
-
89
- end
90
-
91
- private
92
-
93
- def model
94
- self.class.model
95
- end
96
-
97
- def model_params
98
- params.require(model.model_name.singular).permit(self.send("#{model.model_name.singular}_params"))
99
- end
100
-
101
- def model_includes
102
- self.send "#{model.model_name.singular}_includes"
103
- end
104
-
105
- def model_orders
106
- self.send "#{model.model_name.singular}_orders"
107
- end
108
-
109
- def excludes_for(klass)
110
- if defined?(ApplicationHelper) && ApplicationHelper.instance_methods.include?(:excludes)
111
- excludes = Class.new.send(:include, ApplicationHelper).new.excludes.with_indifferent_access
112
- excludes.try(:[], klass.model_name.singular) || []
113
- else
114
- []
115
- end
116
- end
117
-
118
- def model_excludes
119
- excludes_for(model)
120
- end
121
-
122
- def resources
123
- model.filter(params[:where]).where(current_mask[model.table_name])
124
- end
125
-
126
- def includes
127
- @includes ||= StandardAPI::Includes.normalize(params[:include])
128
- end
129
-
130
- def orders
131
- @orders ||= StandardAPI::Orders.sanitize(params[:order], model_orders)
132
- end
133
-
134
- def excludes
135
- @excludes ||= model_excludes
136
- end
137
-
138
- # Used in #calculate
139
- # [{ count: :id }]
140
- # [{ count: '*' }]
141
- # [{ count: '*', maximum: :id, minimum: :id }]
142
- # [{ count: '*' }, { maximum: :id }, { minimum: :id }]
143
- # TODO: Sanitize (normalize_select_params(params[:select], model))
144
- def calculate_selects
145
- return @selects if defined?(@selects)
146
-
147
- functions = ['minimum', 'maximum', 'average', 'sum', 'count']
148
- @selects = []
149
- Array(params[:select]).each do |select|
150
- select.each do |func, column|
151
- column = column == '*' ? Arel.star : column.to_sym
152
- if functions.include?(func.to_s.downcase)
153
- @selects << (model.arel_table[column].send(func).to_sql)
154
- end
155
- end
156
- end
157
-
158
- @selects
159
- end
160
-
161
- end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: standardapi
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.13
4
+ version: 1.0.14
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Bracy
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-09-16 00:00:00.000000000 Z
11
+ date: 2015-09-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord-sort
@@ -189,8 +189,11 @@ extra_rdoc_files:
189
189
  files:
190
190
  - README.md
191
191
  - lib/standard_api.rb
192
+ - lib/standard_api/controller.rb
193
+ - lib/standard_api/helpers.rb
192
194
  - lib/standard_api/includes.rb
193
195
  - lib/standard_api/orders.rb
196
+ - lib/standard_api/railtie.rb
194
197
  - lib/standard_api/test_case.rb
195
198
  - lib/standard_api/test_case/calculate_tests.rb
196
199
  - lib/standard_api/test_case/create_tests.rb