eitil 1.4.1 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d2b711724f69a784e0bc3c374d20fc69a333d7aaf13d14b385eda5e4555f7460
4
- data.tar.gz: 77634b67694ccc4b3491c3109f9f8ce09fb076f7a501bc5bbfee852703b51487
3
+ metadata.gz: fac708d7cfe133d03e39c0606ca98572f8ca1f2dfb490ca872d68fb49b5f054a
4
+ data.tar.gz: 0bf381608da0aaeb140e73e1819a2a5b2f155e6157ab326645b8f8378c2f6f54
5
5
  SHA512:
6
- metadata.gz: 4cdd22e13e5bfb4225187171da7bb0f4fb600c35b72dd007f0bbada16e0d2f349ad97b1c8355aa5e89cd4a6b42b9776027232f278a97aa5e91a2a6bfa5de7922
7
- data.tar.gz: f3084558965176db0aceebb82e218b6dde618e57ab6cb6ab768a9b5f9a36d98f657d28967d08ddcfe5928dc8529382a6971bc1a94a4bc179f4e15c32167dceb1
6
+ metadata.gz: 241e2a5baf494d84d0f2400e06382bbd57d6488b540a68de0eec14c31fe865984dfefff828864e4c816960df303f94f00e4f988c2aec0fc4ce1762b58c156c94
7
+ data.tar.gz: 1309197e2d0bbea822aa36aaec692ed9e903c053eb48130c9d888ddd19aca7632cf15a78d76ca71de7f0cadb89e16dc28fd3ae0cb4e3eaf78601fea07d183e77
@@ -37,44 +37,6 @@ Callback helper methods are created for use within a model's callbacks. You can
37
37
 
38
38
 
39
39
 
40
- ## EitilWrapper::Decorators
41
-
42
- ```ruby
43
-
44
- require "eitil_wrapper/decorators"
45
-
46
- ```
47
-
48
- The Eitil decorator wrappers help you to standardize the calling of the right decorator method from within your controller action. Basically it provides you with a decorate macro in each controller.
49
-
50
- ```ruby
51
- decorate(dec_item, dec_method: nil, dec_class: nil, **dec_kwargs)
52
- ```
53
-
54
- - dec_item is the instance that will be decorated
55
- - dec_method enabled you to set the desired decorator method. If not provided, it will look into the request params: if params["isMobile"] is present it will call .app, if params["isWeb"] is present it will call :app. If neither is provided in the params, it will call the default method :base.
56
- - dec_class enables you to overwrite the decorator class that will be called. If not provided, the decorator class will be inferred from the instance model's classname (User => UserDecorator).
57
- - dec_kwargs enables you to provide additional arguments, which will be passed to your decorator method.
58
-
59
-
60
- ### Configuration
61
-
62
- 1. Your decorator classes should inherit from EitilWrapper::ApplicationDecorator.
63
- 2. Your controllers should inherit the module EitilWrapper::ControllerDecorator, through inclusion in a superclass.
64
- 3. If you set controller ivars for each request, you can make them available in your decorators by providing Eitil with the names of your ivars as an array of symbols:
65
-
66
- ```ruby
67
- # initializers/eitil.rb
68
-
69
- Eitil.set_config do |config|
70
- config.controller_ivars = [:user, :env]
71
- end
72
- ```
73
-
74
-
75
-
76
-
77
-
78
40
  ## EitilWrapper::Jobs
79
41
 
80
42
  ```ruby
@@ -5,5 +5,4 @@ require "eitil_wrapper/jobs"
5
5
  require "eitil_wrapper/records"
6
6
  require "eitil_wrapper/routes"
7
7
  require "eitil_wrapper/callbacks"
8
- require "eitil_wrapper/decorators"
9
8
  require "eitil_wrapper/request_logger"
data/lib/eitil/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module Eitil
2
2
 
3
- VERSION = '1.4.1'
3
+ VERSION = '2.0.0'
4
4
 
5
5
  end
@@ -1,5 +1,3 @@
1
1
  class ApplicationController < ActionController::Base
2
2
 
3
- include EitilWrapper::ControllerDecorator
4
-
5
3
  end
@@ -3,37 +3,6 @@ class UsersController < ApplicationController
3
3
  before_action :set_user, only: [:show, :edit, :update, :destroy]
4
4
 
5
5
 
6
- # ---
7
- # Tests for EitilWrapper::Decorator
8
-
9
- before_action :set_user, only: [:decorator_a, :decorator_b, :decorator_c, :decorator_d]
10
-
11
- # default
12
- def decorator_a
13
- hash = decorate(@user)
14
- render json: hash
15
- end
16
-
17
- # specific dec_method
18
- def decorator_b
19
- hash = decorate(@user, dec_method: :first_and_last_name)
20
- render json: hash
21
- end
22
-
23
- # post request, with 'isWeb' and 'isMobile' params
24
- def decorator_c
25
- hash = decorate(@user)
26
- render json: hash
27
- end
28
-
29
- # specific dec_class
30
- def decorator_d
31
- hash = decorate(@user, dec_class: :Application, dec_method: :timestamps)
32
- render json: hash
33
- end
34
-
35
- # ---
36
-
37
6
 
38
7
 
39
8
  # GET /users
@@ -4,9 +4,4 @@ Rails.application.routes.draw do
4
4
  resources :addresses
5
5
  resources :users
6
6
 
7
- get 'users/:id/decorator_a' => 'users#decorator_a'
8
- get 'users/:id/decorator_b' => 'users#decorator_b'
9
- post 'users/:id/decorator_c' => 'users#decorator_c'
10
- get 'users/:id/decorator_d' => 'users#decorator_d'
11
-
12
7
  end
Binary file
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eitil
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.1
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jurriaan Schrofer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-10-12 00:00:00.000000000 Z
11
+ date: 2021-10-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -275,9 +275,6 @@ files:
275
275
  - eitil_wrapper/lib/eitil_wrapper.rb
276
276
  - eitil_wrapper/lib/eitil_wrapper/callbacks.rb
277
277
  - eitil_wrapper/lib/eitil_wrapper/callbacks/helper_methods.rb
278
- - eitil_wrapper/lib/eitil_wrapper/decorators.rb
279
- - eitil_wrapper/lib/eitil_wrapper/decorators/application_decorator.rb
280
- - eitil_wrapper/lib/eitil_wrapper/decorators/controller_decorator.rb
281
278
  - eitil_wrapper/lib/eitil_wrapper/jobs.rb
282
279
  - eitil_wrapper/lib/eitil_wrapper/jobs/new_job.rb
283
280
  - eitil_wrapper/lib/eitil_wrapper/jobs/new_job_now.rb
@@ -309,9 +306,6 @@ files:
309
306
  - spec/dummy_app/app/controllers/addresses_controller.rb
310
307
  - spec/dummy_app/app/controllers/application_controller.rb
311
308
  - spec/dummy_app/app/controllers/users_controller.rb
312
- - spec/dummy_app/app/decorators/address_decorator.rb
313
- - spec/dummy_app/app/decorators/application_decorator.rb
314
- - spec/dummy_app/app/decorators/user_decorator.rb
315
309
  - spec/dummy_app/app/helpers/addresses_helper.rb
316
310
  - spec/dummy_app/app/helpers/application_helper.rb
317
311
  - spec/dummy_app/app/helpers/users_helper.rb
@@ -444,7 +438,6 @@ files:
444
438
  - spec/eitil_integrate/application_exporter/auto_sum_spec.rb
445
439
  - spec/eitil_integrate/application_exporter/initialize_spec.rb
446
440
  - spec/eitil_wrapper/callbacks/helper_methods_spec.rb
447
- - spec/eitil_wrapper/decorators/decorators_spec.rb
448
441
  - spec/eitil_wrapper/jobs/single_method_job_spec.rb
449
442
  - spec/eitil_wrapper/routes/extended_resources_spec.rb
450
443
  - spec/eitil_wrapper/scopes/default_scopes_spec.rb
@@ -1,22 +0,0 @@
1
-
2
- require "active_model/railtie"
3
-
4
- module EitilWrapper
5
- class ApplicationDecorator
6
-
7
- include ActiveModel::Model
8
-
9
- def initialize(attributes={}, **kwargs)
10
- super attributes
11
- all_kwargs_to_ivars binding
12
- end
13
-
14
- def self.method_missing(method_name, *args, **kwargs)
15
- if %i$ web app $.include?(method_name.to_sym)
16
- kwargs.any? ? send(:base, *args, **kwargs) : send(:base, *args)
17
- end
18
- super
19
- end
20
-
21
- end
22
- end
@@ -1,72 +0,0 @@
1
- module EitilWrapper
2
- module ControllerDecorator
3
-
4
- private
5
-
6
- def decorate(dec_item, dec_method: nil, dec_class: nil, **dec_kwargs)
7
- all_args_to_ivars binding
8
- set_ivars :dec_class, :decorator, :dec_method
9
- send_to_decorator
10
-
11
- rescue NameError => e
12
- puts "EitilWrapper::ControllerDecorator => rescuing uninitialised constant #{e.name}, returning the object's default JSON instead."
13
- return dec_item.as_json
14
- end
15
-
16
- def send_to_decorator
17
- @decorator.send(@dec_method, @dec_item)
18
-
19
- rescue NoMethodError => e
20
- inform_no_method_for_decorator_error
21
- @dec_item
22
- end
23
-
24
- def inform_no_method_for_decorator_error
25
- message = "Warning: NoMethodError for #{@dec_class}##{@dec_method}, returned @dec_item instead."
26
- Logger.new("#{Rails.root}/log/decorator_log.log").warn message
27
- warn message
28
- end
29
-
30
- def set_dec_method
31
- @dec_method = @dec_method || derived_dec_method || :base
32
- end
33
-
34
- def derived_dec_method
35
- return unless respond_to? :params
36
- return :app if @decorator.respond_to?(:app) && params["isMobile"]
37
- return :web if @decorator.respond_to?(:web) && params["isWeb"]
38
- end
39
-
40
- def set_dec_class
41
- @dec_class = @dec_class ? manual_set_dec_class : derived_dec_class
42
- end
43
-
44
- def manual_set_dec_class
45
- "#{@dec_class}Decorator".constantize
46
- end
47
-
48
- def derived_dec_class
49
- "#{@dec_item.class.name}Decorator".constantize
50
- end
51
-
52
- def set_decorator
53
- @dec_class.new **ivars
54
- end
55
-
56
- # Converts the controller_ivars, given in your main application's configuration,
57
- # into an hash. If no controller_ivars, continue with converting, in order to avoid
58
- # issues due to differing data types.
59
- def controller_ivars
60
- return {} unless Eitil.controller_ivars.any?
61
-
62
- Eitil.controller_ivars.map do |ivar|
63
- { ivar => instance_variable_get("@#{ivar.to_s}") }
64
- end.inject &:merge
65
- end
66
-
67
- def ivars
68
- @dec_kwargs.any? ? controller_ivars.merge(@dec_kwargs) : controller_ivars
69
- end
70
-
71
- end
72
- end
@@ -1,3 +0,0 @@
1
-
2
- require "eitil_wrapper/decorators/application_decorator"
3
- require "eitil_wrapper/decorators/controller_decorator"
@@ -1,3 +0,0 @@
1
- class AddressDecorator < ApplicationDecorator
2
-
3
- end
@@ -1,12 +0,0 @@
1
- class ApplicationDecorator < EitilWrapper::ApplicationDecorator
2
-
3
- # ---
4
- # Tests for EitilWrapper::Decorator
5
-
6
- def timestamps(record)
7
- record.slice(:created_at, :updated_at)
8
- end
9
-
10
- # ---
11
-
12
- end
@@ -1,19 +0,0 @@
1
- class UserDecorator < ApplicationDecorator
2
-
3
- def first_and_last_name(user)
4
- user.slice(:first_name, :last_name)
5
- end
6
-
7
- def app(user)
8
- user.slice(:phone, :email)
9
- end
10
-
11
- def web(user)
12
- user.slice(:age, :wage)
13
- end
14
-
15
- def base(user)
16
- user.as_json
17
- end
18
-
19
- end
@@ -1,49 +0,0 @@
1
-
2
- RSpec.describe "EitilWrapper::Decorators" do
3
-
4
- include Rack::Test::Methods
5
-
6
- let(:user) { User.create(first_name: 'slim', last_name: 'shady') }
7
-
8
- def body(endpoint)
9
- @body ||= JSON.parse(get("/users/#{user.id}/#{endpoint}").body)
10
- end
11
-
12
- it "should respond to a request" do
13
- response = get "/users/#{user.id}/decorator_a"
14
- expect(response).to be_a Rack::MockResponse
15
- end
16
-
17
- it "should, by default, return all of a records' columns" do
18
- response_keys = body(:decorator_a).keys.map(&:to_sym)
19
- table_columns = [:id, :created_at, :updated_at, :first_name, :last_name, :email, :birthday, :phone, :confirmed, :last_sign_in, :age, :wage]
20
- expect(table_columns.all? { |column| response_keys.include?(column) }).to be_truthy
21
- end
22
-
23
- it "should, if a specific decorator method is passed, return that method's value" do
24
- response_keys = body(:decorator_b).keys.map(&:to_sym)
25
- decorator_keys = [:first_name, :last_name]
26
- expect(response_keys).to eq decorator_keys
27
- end
28
-
29
- it "should, if no decorator method is passed, but param['isMobile'] is present, return the :app method's value" do
30
- response = post "/users/#{user.id}/decorator_c", { "isMobile" => true }
31
- response_keys = JSON.parse(response.body).keys.map(&:to_sym)
32
- decorator_keys = [:phone, :email]
33
- expect(response_keys).to eq decorator_keys
34
- end
35
-
36
- it "should, if no decorator method is passed, but param['isWeb'] is present, return the :web method's value" do
37
- response = post "/users/#{user.id}/decorator_c", { "isWeb" => true }
38
- response_keys = JSON.parse(response.body).keys.map(&:to_sym)
39
- decorator_keys = [:age, :wage]
40
- expect(response_keys).to eq decorator_keys
41
- end
42
-
43
- it "should, if a decorator class is passed, return the value of that class' method" do
44
- response_keys = body(:decorator_d).keys.map(&:to_sym)
45
- decorator_keys = [:created_at, :updated_at]
46
- expect(response_keys).to eq decorator_keys
47
- end
48
-
49
- end