rails-add_ons 0.2.0 → 0.3.0

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: 963bb8277207578ae5916394485eb2adb8c0e8ea
4
- data.tar.gz: 1ab4b5ac4548ec0f88f66cf99ab712c22d88f331
3
+ metadata.gz: ec60bb83abbf6700fdc1e9d9bad0aeab41c0d42d
4
+ data.tar.gz: 6768f210ab47e3c60bf57dcfecea186f1a4998e8
5
5
  SHA512:
6
- metadata.gz: e7e66a7c872436641d6f8e28c2b23d891d3b2c0122153e68ad57b2ab9817d56239d00dd42ceeec001c337740c371201db922c233ee3e27be0ea8a13896a664a2
7
- data.tar.gz: d09be621db5157cac495193403b0e8e88f9672c3b424f0aefdc1f0e14163db67e5ccb478c79b9fefa3d9e563efbe6ee089749ca22c0b98d6f97df1d229ad225a
6
+ metadata.gz: 12e994afc0b6ab5eb2f0731f7c1454866b2af87b19b7f95dc38afdeee55d3854aaad261e97864c3a2141cd96b0c76f2977b8165b04a7253052b759964a67a290
7
+ data.tar.gz: 6756eff8bcde2f8b3fe5c1b638815215132e660864edefe8f539eecd5a263a00014308d6034e708c9f4e0c233150fcac0d6d23ad40e5ec9aa124ad3ec559280f
@@ -112,9 +112,9 @@ module Api
112
112
  request.query_parameters.each do |field, condition|
113
113
  case field
114
114
  when 'limit'
115
- scope = scope.limit(condition)
115
+ scope = scope.limit(condition.to_i)
116
116
  when 'offset'
117
- scope = scope.offset(condition)
117
+ scope = scope.offset(condition.to_i)
118
118
  when 'order'
119
119
  scope = scope.order(condition)
120
120
  when 'includes'
@@ -190,7 +190,9 @@ module Api
190
190
  private
191
191
 
192
192
  def load_count
193
- @count = resource_class.count
193
+ base_scope = resource_class
194
+ scope = add_conditions_from_query(base_scope)
195
+ @count = scope.count
194
196
  end
195
197
  end
196
198
 
@@ -241,6 +243,67 @@ module Api
241
243
  @count = resource_class.delete_all
242
244
  end
243
245
  end
246
+
247
+ module FirstAction
248
+ extend ActiveSupport::Concern
249
+
250
+ included do
251
+ if respond_to?(:before_action)
252
+ before_action :load_first, only: [:first]
253
+ else
254
+ before_filter :load_first, only: [:first]
255
+ end
256
+ end
257
+
258
+ def first
259
+ respond_to do |format|
260
+ if @resource.nil?
261
+ format.json { render json: nil }
262
+ else
263
+ format.json { render json: [serialize_resource(@resource)] }
264
+ end
265
+ end
266
+ end
267
+
268
+ private
269
+
270
+ def load_first
271
+ base_scope = resource_class
272
+ scope = add_conditions_from_query(base_scope)
273
+ @resource = scope.first
274
+ end
275
+ end
276
+
277
+ module LastAction
278
+ extend ActiveSupport::Concern
279
+
280
+ included do
281
+ if respond_to?(:before_action)
282
+ before_action :load_last, only: [:last]
283
+ else
284
+ before_filter :load_last, only: [:last]
285
+ end
286
+ end
287
+
288
+ def last
289
+ respond_to do |format|
290
+ if @resource.nil?
291
+ format.json { render json: nil }
292
+ else
293
+ format.json { render json: [serialize_resource(@resource)] }
294
+ end
295
+ end
296
+ end
297
+
298
+ private
299
+
300
+ def load_last
301
+ base_scope = resource_class
302
+ scope = add_conditions_from_query(base_scope)
303
+ @resource = scope.last
304
+ end
305
+ end
306
+
244
307
  include RestActions
245
308
  include Resources
246
309
  include RestResourceUrls
@@ -248,6 +311,8 @@ module Api
248
311
  include CountAction
249
312
  include DestroyAllAction
250
313
  include DeleteAllAction
314
+ include FirstAction
315
+ include LastAction
251
316
  include ApiControllerConcerns::ExceptionHandling
252
317
  end
253
318
  end
@@ -8,7 +8,7 @@ module Api
8
8
  include ActionController::MimeResponds
9
9
 
10
10
  respond_to :json
11
-
11
+
12
12
  if respond_to?(:before_action)
13
13
  before_action :initialize_service_for_create, only: [:create]
14
14
  else
@@ -17,6 +17,12 @@ module Api
17
17
  end
18
18
 
19
19
  def create
20
+ perform
21
+ end
22
+
23
+ private
24
+
25
+ def perform
20
26
  @result = @service.perform
21
27
  respond_to do |format|
22
28
  if @result.success?
@@ -27,16 +33,17 @@ module Api
27
33
  end
28
34
  end
29
35
 
30
- private
31
-
32
36
  def initialize_service_for_create
37
+ @service = service_class.new(*service_arguments)
38
+ end
39
+
40
+ def service_arguments
33
41
  # In rails 5 permitted_params is an instance of ActionController::Parameters.
34
42
  # Stragely, when calling #delete on it, it does not delete the key, so we have
35
- # to transform it into a hash first.
43
+ # to transform it into a hash first.
36
44
  params_hash = permitted_params.try(:to_h).presence || permitted_params
37
-
38
45
  options = params_hash.try(:delete, :options) || {}
39
- @service = service_class.new(params_hash, options)
46
+ [params_hash, options]
40
47
  end
41
48
 
42
49
  def permitted_params
@@ -24,16 +24,20 @@ module ServiceController
24
24
  def new; end
25
25
 
26
26
  def create
27
- @response = @resource.perform
28
- if @response.success?
27
+ perform
28
+ end
29
+
30
+ private
31
+
32
+ def perform
33
+ @result = @resource.perform
34
+ if @result.success?
29
35
  render :create
30
36
  else
31
37
  render :new
32
38
  end
33
39
  end
34
40
 
35
- private
36
-
37
41
  def initialize_service
38
42
  @resource = service_class.new
39
43
  end
@@ -132,4 +136,4 @@ module ServiceController
132
136
  include ResourceInflections
133
137
  include LocationHistory
134
138
  end
135
- end
139
+ end
@@ -52,7 +52,7 @@ module Api
52
52
  end
53
53
 
54
54
  def extract_column_and_operator(string)
55
- if string =~ /([a-z_]{1,})\(([a-z_]{2,})\)/
55
+ if string =~ /([\.a-z_]{1,})\(([a-z_]{2,})\)/
56
56
  return $~[1], $~[2]
57
57
  end
58
58
  end
@@ -3,6 +3,7 @@ module Rails
3
3
  module Service
4
4
  class Base
5
5
  include ActiveModel::Model
6
+ extend ActiveModel::Naming
6
7
 
7
8
  def self.attr_accessor(*args)
8
9
  super
@@ -19,10 +20,6 @@ module Rails
19
20
  (@attr_names ||= [])
20
21
  end
21
22
 
22
- def self.i18n_scope
23
- :activerecord
24
- end
25
-
26
23
  def self.call(*args)
27
24
  new(*args).perform
28
25
  end
@@ -149,7 +146,7 @@ module Rails
149
146
  include Errors
150
147
  include Resultable
151
148
  include Callbacks
152
- include Messages
149
+ include Rails::AddOns::Service::Messages
153
150
  end
154
151
  end
155
152
  end
@@ -0,0 +1,44 @@
1
+ module Rails
2
+ module AddOns
3
+ module Service
4
+ module Messages
5
+ private
6
+
7
+ def initialize_messages
8
+ @messages = []
9
+ end
10
+
11
+ def say(what, &block)
12
+ @indent ||= 0
13
+ if block_given?
14
+ @indent += 1
15
+ output "#{output_prefix}#{(" " * @indent)}#{what}..."
16
+ block_result = yield
17
+ say_done
18
+ @indent -= 1
19
+ block_result
20
+ else
21
+ output "#{output_prefix}#{(" " * @indent)}#{what}"
22
+ end
23
+ end
24
+
25
+ def say_done
26
+ say " => Done"
27
+ end
28
+
29
+ def output_prefix
30
+ "[#{self.class.name}] "
31
+ end
32
+
33
+ def output(what)
34
+ @messages << what
35
+ puts what
36
+ end
37
+
38
+ def copy_messages_to_result
39
+ @result.instance_variable_set(:@messages, @messages)
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
File without changes
File without changes
File without changes
File without changes
@@ -1,6 +1,7 @@
1
1
  !!!
2
2
  %html{:lang => "en"}
3
3
  %head
4
+ = render 'before_head'
4
5
  = csrf_meta_tags
5
6
 
6
7
  / Required meta tags
@@ -8,16 +9,19 @@
8
9
  %meta{:content => "width=device-width, initial-scale=1, shrink-to-fit=no", :name => "viewport"}/
9
10
  / Bootstrap CSS
10
11
  %link{:crossorigin => "anonymous", :href => "https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css", :integrity => "sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ", :rel => "stylesheet"}/
11
- = stylesheet_link_tag "rails/add_ons/application", media: 'all', :'data-turbolinks-track' => 'reload'
12
+ = stylesheet_link_tag "rails/add_ons/application", media: 'all', :'data-turbolinks-track' => 'reload'
12
13
  = stylesheet_link_tag "rails/add_ons/font_awesome", media: 'all', :'data-turbolinks-track' => 'reload'
13
14
  = javascript_include_tag "rails/add_ons/application", :'data-turbolinks-track' => 'reload'
14
15
  / widget endpoint
15
16
  - if respond_to?(:rails_add_ons)
16
17
  %meta{name: 'widget-base-path', content: rails_add_ons.widget_path}
18
+ = render 'after_head'
17
19
  %body
20
+ = render 'before_body'
18
21
  = render 'navbar'
19
22
  .container-fluid
20
23
  .starter-template
21
24
  = bootstrap_flash
22
25
  = yield
23
- / /.container
26
+ / /.container
27
+ = render 'after_body'
@@ -1,13 +1,13 @@
1
- = render 'create_before_service_output', response: @response
1
+ = render 'create_before_service_output', result: @result
2
2
 
3
3
  %table.table.table-sm.table-striped.table-monospaced
4
4
  %tbody
5
- - @response.messages.each_with_index do |message, index|
5
+ - @result.messages.each_with_index do |message, index|
6
6
  %tr
7
7
  %td= index + 1
8
8
  %td= message
9
9
 
10
- = render 'create_after_service_output', response: @response
10
+ = render 'create_after_service_output', result: @result
11
11
 
12
12
  = bootstrap_button(to: new_resource_path, context: :success) do
13
13
  = fa_icon :back
@@ -15,6 +15,9 @@ de:
15
15
  destroy:
16
16
  notice: "%{resource_name} wurde gelöscht."
17
17
  alert: "%{resource_name} konnte nicht gelöscht werden."
18
+ perform:
19
+ notice: "%{resource_name} wurde ausgeführt."
20
+ alert: "%{resource_name} konnte nicht ausgeführt werden. Es traten folgende Fehler auf: %{errors}"
18
21
  i18n:
19
22
  locales:
20
23
  en: English
@@ -36,4 +39,8 @@ de:
36
39
  create:
37
40
  back: "Zurück"
38
41
  new:
39
- title: "%{service_name} ausführen"
42
+ title: "%{service_name} ausführen"
43
+ views:
44
+ pagination:
45
+ last: '&#9654;'
46
+ next: '&#9664;'
@@ -15,6 +15,9 @@ en:
15
15
  destroy:
16
16
  notice: "%{resource_name} was successfully destroyed."
17
17
  alert: "%{resource_name} could not be destroyed."
18
+ perform:
19
+ notice: "%{resource_name} was executed."
20
+ alert: "%{resource_name} could not be executed. Errors: %{errors}"
18
21
  i18n:
19
22
  locales:
20
23
  de: Deutsch
@@ -36,4 +39,8 @@ en:
36
39
  create:
37
40
  back: "Back"
38
41
  new:
39
- title: "Run %{service_name}"
42
+ title: "Run %{service_name}"
43
+ views:
44
+ pagination:
45
+ last: '&#9654;'
46
+ next: '&#9664;'
@@ -0,0 +1,98 @@
1
+ module ActiveModel
2
+
3
+ # == Active Model Basic Model
4
+ #
5
+ # Includes the required interface for an object to interact with
6
+ # <tt>ActionPack</tt>, using different <tt>ActiveModel</tt> modules.
7
+ # It includes model name introspections, conversions, translations and
8
+ # validations. Besides that, it allows you to initialize the object with a
9
+ # hash of attributes, pretty much like <tt>ActiveRecord</tt> does.
10
+ #
11
+ # A minimal implementation could be:
12
+ #
13
+ # class Person
14
+ # include ActiveModel::Model
15
+ # attr_accessor :name, :age
16
+ # end
17
+ #
18
+ # person = Person.new(name: 'bob', age: '18')
19
+ # person.name # => 'bob'
20
+ # person.age # => 18
21
+ #
22
+ # Note that, by default, <tt>ActiveModel::Model</tt> implements <tt>persisted?</tt>
23
+ # to return +false+, which is the most common case. You may want to override
24
+ # it in your class to simulate a different scenario:
25
+ #
26
+ # class Person
27
+ # include ActiveModel::Model
28
+ # attr_accessor :id, :name
29
+ #
30
+ # def persisted?
31
+ # self.id == 1
32
+ # end
33
+ # end
34
+ #
35
+ # person = Person.new(id: 1, name: 'bob')
36
+ # person.persisted? # => true
37
+ #
38
+ # Also, if for some reason you need to run code on <tt>initialize</tt>, make
39
+ # sure you call +super+ if you want the attributes hash initialization to
40
+ # happen.
41
+ #
42
+ # class Person
43
+ # include ActiveModel::Model
44
+ # attr_accessor :id, :name, :omg
45
+ #
46
+ # def initialize(attributes={})
47
+ # super
48
+ # @omg ||= true
49
+ # end
50
+ # end
51
+ #
52
+ # person = Person.new(id: 1, name: 'bob')
53
+ # person.omg # => true
54
+ #
55
+ # For more detailed information on other functionalities available, please
56
+ # refer to the specific modules included in <tt>ActiveModel::Model</tt>
57
+ # (see below).
58
+ module Model
59
+ def self.included(base) #:nodoc:
60
+ base.class_eval do
61
+ extend ActiveModel::Naming
62
+ extend ActiveModel::Translation
63
+ include ActiveModel::Validations
64
+ include ActiveModel::Conversion
65
+ end
66
+ end
67
+
68
+ # Initializes a new model with the given +params+.
69
+ #
70
+ # class Person
71
+ # include ActiveModel::Model
72
+ # attr_accessor :name, :age
73
+ # end
74
+ #
75
+ # person = Person.new(name: 'bob', age: '18')
76
+ # person.name # => "bob"
77
+ # person.age # => 18
78
+ def initialize(params={})
79
+ params.each do |attr, value|
80
+ # self.public_send("#{attr}=", value)
81
+ self.send("#{attr}=", value)
82
+ end if params
83
+ end
84
+
85
+ # Indicates if the model is persisted. Default is +false+.
86
+ #
87
+ # class Person
88
+ # include ActiveModel::Model
89
+ # attr_accessor :id, :name
90
+ # end
91
+ #
92
+ # person = Person.new(id: 1, name: 'bob')
93
+ # person.persisted? # => false
94
+ def persisted?
95
+ false
96
+ end
97
+ end
98
+ end
@@ -1,5 +1,5 @@
1
1
  module Rails
2
2
  module AddOns
3
- VERSION = '0.2.0'
3
+ VERSION = '0.3.0'
4
4
  end
5
5
  end
data/lib/rails-add_ons.rb CHANGED
@@ -4,5 +4,6 @@ require 'twitter_bootstrap_components_rails'
4
4
  require 'simple_form'
5
5
  require 'responders' if Rails::VERSION::MAJOR > 3
6
6
  require 'rails-i18n'
7
+ require 'active_model/model' if Rails::VERSION::MAJOR < 4
7
8
 
8
9
  require 'rails/add_ons'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails-add_ons
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Roberto Vasquez Angel
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-05-23 00:00:00.000000000 Z
11
+ date: 2017-06-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -202,9 +202,14 @@ files:
202
202
  - app/helpers/rails/add_ons/widget_helper.rb
203
203
  - app/parsers/api/resources_controller/condition_parser.rb
204
204
  - app/services/rails/add_ons/service/base.rb
205
+ - app/services/rails/add_ons/service/messages.rb
205
206
  - app/services/rails/add_ons/service/result/base.rb
206
207
  - app/views/component/_collection_table.haml
207
208
  - app/views/component/_resource_table.haml
209
+ - app/views/frontend/_after_body.haml
210
+ - app/views/frontend/_after_head.haml
211
+ - app/views/frontend/_before_body.haml
212
+ - app/views/frontend/_before_head.haml
208
213
  - app/views/frontend/_navbar.haml
209
214
  - app/views/layouts/rails/add_ons/application.haml
210
215
  - app/views/resources_controller/base/_after_show_table.haml
@@ -236,6 +241,7 @@ files:
236
241
  - config/locales/de.yml
237
242
  - config/locales/en.yml
238
243
  - config/routes.rb
244
+ - lib/active_model/model.rb
239
245
  - lib/rails-add_ons.rb
240
246
  - lib/rails/add_ons.rb
241
247
  - lib/rails/add_ons/engine.rb