arkaan 2.6.0 → 2.7.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
  SHA256:
3
- metadata.gz: a4990a4abb8ed39b023c47fed7560e0cad8aea739b74ed3a849ede6cf903f655
4
- data.tar.gz: c83e4c79e96321d68b2a95774d7203a649850c27883b4bd0aa2f758c9d7bc9ea
3
+ metadata.gz: 15f29a2d1dc424ac2db2aed441e007437b4aab5fe6030711f89e8afe3ca7d1ed
4
+ data.tar.gz: c8f989b214ae66572e0b06249a95362642341c4a34ffe63c2eacb4e33e264150
5
5
  SHA512:
6
- metadata.gz: 511fac191930fd795fc4f3961a249ef44159b0f21680f3a009efdd64b3e80c60a2de1046e938b73ae2eb6889a61e8355889367b6cb868ca2eb4f97eedd730c17
7
- data.tar.gz: 72b3aa7b65f4f2bc142c5cab8c4859ca569e57f978d261c50c20c7495724dadbb33f554a9eaf911c12b7774f9f7690bef956891d10250965383d901c748c1b35
6
+ metadata.gz: 01dd270f06012cce7c1aef0e4da22deb04698e36312c1a306b9c465431fdf76cb267f86b4f616bbb8895d3b850ec3a6a9afd21e5c829d103e81ef1cbf0305952
7
+ data.tar.gz: 3e5a3d6cd2fdaff73b2cc88d754040267de4db4fb100ed4977a5f8ab106ccae7ba2b2e5fdfb2663b0f2fd07d87150bbb4a6e5783a7087d8019e4941eb00025e0
@@ -13,6 +13,7 @@ module Arkaan
13
13
  autoload :Chatroom , 'arkaan/chatroom'
14
14
  autoload :Chatrooms , 'arkaan/chatrooms'
15
15
  autoload :Concerns , 'arkaan/concerns'
16
+ autoload :Event , 'arkaan/event'
16
17
  autoload :Factories , 'arkaan/factories'
17
18
  autoload :Files , 'arkaan/files'
18
19
  autoload :Monitoring , 'arkaan/monitoring'
@@ -51,14 +51,15 @@ module Arkaan
51
51
  # @param account [Arkaan::Account] the account of the creator for this campaign.
52
52
  def creator=(account)
53
53
  if !invitations.where(account: account).exists?
54
- Arkaan::Campaigns::Invitation.create(campaign: self, account: account, enum_status: :creator)
54
+ invitation = Arkaan::Campaigns::Invitation.create(campaign: self, account: account)
55
+ invitation.status = :creator
55
56
  end
56
57
  end
57
58
 
58
59
  # Getter for the creator account of this campaign.
59
60
  # @return [Arkaan::Account] the account of the player creating this campaign.
60
61
  def creator
61
- return invitations.where(enum_status: :creator).first.account
62
+ invitations.to_a.find(&:status_creator?).account
62
63
  end
63
64
 
64
65
  # Adds an error message if the account creating this campaign already has a campaign with the very same name.
@@ -83,7 +84,9 @@ module Arkaan
83
84
 
84
85
  # @return [Array<Arkaan::Campaigns::Invitation>] the players in this campaign.
85
86
  def players
86
- invitations.where(enum_status: :accepted)
87
+ invitations.to_a.select do |invitation|
88
+ [:creator, :accepted].include? invitation.enum_status
89
+ end
87
90
  end
88
91
 
89
92
  # @return [Integer] the number of players in this campaign.
@@ -1,15 +1,16 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Arkaan
2
4
  module Campaigns
3
- # An invitation is the linked between a player and a campaign, accepted or not.
5
+ # An invitation is the linked between a player and a campaign.
6
+ # It keeps the history of the interaction between the player and the campaign.
7
+ #
4
8
  # @author Vincent Courtois <courtois.vincent@outlook.com>
5
9
  class Invitation
6
10
  include Mongoid::Document
7
11
  include Mongoid::Timestamps
8
12
  include Arkaan::Concerns::Enumerable
9
-
10
- # @!attribute [rw] status
11
- # @return [Symbol] the current status of the invitation.
12
- enum_field :status, [:accepted, :blocked, :expelled, :ignored, :left, :pending, :refused, :request, :creator], default: :pending
13
+ include Arkaan::Concerns::Historizable
13
14
 
14
15
  # @!attribute [rw] account
15
16
  # @return [Arkaan::Account] the account the invitation has been issued to.
@@ -17,6 +18,12 @@ module Arkaan
17
18
  # @!attribute [rw] campaign
18
19
  # @return [Arkaan::Campaign] the campaign the invitation has been made in.
19
20
  belongs_to :campaign, class_name: 'Arkaan::Campaign', inverse_of: :invitations
21
+
22
+ # @!attribute [rw] status
23
+ # @return [Symbol] the current status of the invitation.
24
+ historize enum_field :status,
25
+ [:pending, :request, :accepted, :refused, :expelled, :left, :master, :creator],
26
+ default: :pending
20
27
  end
21
28
  end
22
- end
29
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Arkaan
2
4
  module Campaigns
3
5
  # A campaign tag is a string describing a characteristic of the campaign it's in.
@@ -13,9 +15,7 @@ module Arkaan
13
15
  # @return [Integer] the number of campaigns this tag is in, avoiding a join.
14
16
  field :count, type: Integer, default: 1
15
17
 
16
- validates :content,
17
- presence: {message: 'required'},
18
- uniqueness: {message: 'uniq'}
18
+ validates :content, presence: { message: 'required' }, uniqueness: { message: 'uniq' }
19
19
  end
20
20
  end
21
- end
21
+ end
@@ -5,6 +5,7 @@ module Arkaan
5
5
  autoload :Activable , 'arkaan/concerns/activable'
6
6
  autoload :Diagnosticable, 'arkaan/concerns/diagnosticable'
7
7
  autoload :Enumerable , 'arkaan/concerns/enumerable'
8
+ autoload :Historizable , 'arkaan/concerns/historizable'
8
9
  autoload :MimeTypable , 'arkaan/concerns/mime_typable'
9
10
  autoload :Premiumable , 'arkaan/concerns/premiumable'
10
11
  autoload :Sluggable , 'arkaan/concerns/sluggable'
@@ -14,7 +14,7 @@ module Arkaan
14
14
  # @param values [Array<Symbol>] the possible values of the enumerated field.
15
15
  # @param options [Hash<Symbol, Any>] the possible options for the field.
16
16
  def enum_field(field_name, values, options = {})
17
- field :"enum_#{field_name}", type: Symbol, default: options[:default]
17
+ returned = field :"enum_#{field_name}", type: Symbol, default: options[:default]
18
18
 
19
19
  validates :"enum_#{field_name}", inclusion: {in: values.map(&:to_sym), message: 'inclusion'}
20
20
 
@@ -37,6 +37,10 @@ module Arkaan
37
37
  self["enum_#{field_name}"] == value
38
38
  end
39
39
  end
40
+
41
+ # This is to make enumerations historizable by
42
+ # returning the field object created by Mongoid.
43
+ returned
40
44
  end
41
45
  end
42
46
  end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Arkaan
4
+ module Concerns
5
+ # This module is what I call "a beautiful piece of Ruby engineering"
6
+ # It takes any mongoid field that you may have declared, and historizes
7
+ # it in a dedicated relation if this relation does not already exists.
8
+ #
9
+ # What it does exactly :
10
+ # - Creates the :history relation if it does not already exists.
11
+ # - Creates a method to check for changes of a specific attribute.
12
+ # - Check for changes at initialization to insert the first value.
13
+ # - Check for changes at update/save to store the history.
14
+ #
15
+ # @author Vincent Courtois <courtois.vincent@outlook.com>
16
+ module Historizable
17
+ extend ActiveSupport::Concern
18
+
19
+ # Adds an entry in the history table for the given field.
20
+ # It checks several things to make the history entry valid :
21
+ # - the new value is different from the old value
22
+ # - the old value is identical to the last recorded new value.
23
+ #
24
+ # @param field [String] the name of the field to historize
25
+ # @param from [Any] the old value before update
26
+ # @param to [Any] the new value after update.
27
+ def add_history(field:, from:, to:)
28
+ return if from == to
29
+ return if !history.empty? && history.order_by(:created_at.desc).first.to != from
30
+
31
+ event = Arkaan::Event.create(field: field, from: from, to: to, document: self)
32
+ event.save
33
+ end
34
+
35
+ # Submodule holding all the static methods add to the current subclass.
36
+ # @author Vincent Courtois <courtois.vincent@outlook.com>
37
+ module ClassMethods
38
+
39
+ # Takes the Mongoid declared field and creates the callbacks
40
+ # to intercept any value change and add it to the history.
41
+ # @field field [Mongoid::Fields::Standard] the Mongoid field to historize.
42
+ def historize(field)
43
+
44
+ unless relations.key?('history')
45
+ embeds_many :history, class_name: 'Arkaan::Event'
46
+ end
47
+
48
+ after_initialize do |doc|
49
+ add_history(field: field.name, from: nil, to: doc[field.name])
50
+ end
51
+
52
+ after_save do |doc|
53
+ if doc.changed_attributes.key?(field.name)
54
+ from = doc.changed_attributes[field.name]
55
+ add_history(field: field.name, from: from, to: doc[field.name])
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Arkaan
4
+ # An event is symbolizing a timestamped change in a model.
5
+ # It is recommended NOT to use this class directly but to use
6
+ # the Arkaan::Concerns::Historizable concern in a model.
7
+ #
8
+ # @author Vincent Courtois <courtois.vincent@outlook.com>
9
+ class Event
10
+ include Mongoid::Document
11
+ include Mongoid::Timestamps
12
+
13
+ # @!attribute [rw] field
14
+ # @return [String] the name of the field being historized
15
+ field :field, type: String
16
+ # @!attribute [rw] from
17
+ # @return [Any] the value of the field before update
18
+ field :from
19
+ # @!attribute [rw] to
20
+ # @return [Any] the value of the field after update
21
+ field :to
22
+
23
+ # @!attribute [rw] document
24
+ # @return [Any] the model in which the history is embedded
25
+ embedded_in :document, polymorphic: true, inverse_of: :history
26
+
27
+ validates :field, presence: { message: 'required' }
28
+ end
29
+ end
@@ -1,6 +1,7 @@
1
1
  module Arkaan
2
2
  module OAuth
3
- # An access token is the value assigned to the application to access the private data of an account.
3
+ # An access token is the value assigned to the application
4
+ # to access the data the user is allowed to access.
4
5
  # @author Vincent Courtois <courtois.vincent@outlook.com>
5
6
  class AccessToken
6
7
  include Mongoid::Document
@@ -15,11 +16,17 @@ module Arkaan
15
16
 
16
17
  # @!attribute [rw] authorization
17
18
  # @return [Arkaan::OAuth::Authorization] the authorization code that issued this token to the application for this user.
18
- belongs_to :authorization, class_name: 'Arkaan::OAuth::Authorization', inverse_of: :access_token
19
+ belongs_to :authorization, class_name: 'Arkaan::OAuth::Authorization', inverse_of: :tokens
19
20
 
20
21
  validates :value,
21
22
  presence: {message: 'required'},
22
23
  uniqueness: {message: 'uniq'}
24
+
25
+ # Checks if the current date is inferior to the creation date + expiration period
26
+ # @return [Boolean] TRUE if the token is expired, FALSE otherwise.
27
+ def expired?
28
+ created_at.to_time.to_i + expiration < Time.now.to_i
29
+ end
23
30
  end
24
31
  end
25
32
  end
@@ -1,6 +1,9 @@
1
1
  module Arkaan
2
2
  module OAuth
3
3
  # An OAuth authorization is granted by a user to an application to access its personal data.
4
+ # The application then transforms it into an access token to be able to send it with
5
+ # further requests, so that we know the user has authorized the application to access its data.
6
+ #
4
7
  # @author Vincent Courtois <courtois.vincent@outlook.com>
5
8
  class Authorization
6
9
  include Mongoid::Document
@@ -18,7 +21,7 @@ module Arkaan
18
21
  belongs_to :application, class_name: 'Arkaan::OAuth::Application', inverse_of: :authorizations
19
22
  # @!attribute [rw] token
20
23
  # @return [Arkaan::OAuth::AccessToken] the access token used further in the application process to access private data of the account.
21
- has_one :token, class_name: 'Arkaan::OAuth::AccessToken', inverse_of: :authorization
24
+ has_many :tokens, class_name: 'Arkaan::OAuth::AccessToken', inverse_of: :authorization
22
25
 
23
26
  validates :code,
24
27
  presence: {message: 'required'},
@@ -1,3 +1,3 @@
1
1
  module Arkaan
2
- VERSION = '2.6.0'
2
+ VERSION = '2.7.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: arkaan
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.6.0
4
+ version: 2.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vincent Courtois
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-03-24 00:00:00.000000000 Z
11
+ date: 2020-06-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -164,6 +164,20 @@ dependencies:
164
164
  - - '='
165
165
  - !ruby/object:Gem::Version
166
166
  version: 2.10.0
167
+ - !ruby/object:Gem::Dependency
168
+ name: rubocop
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - '='
172
+ - !ruby/object:Gem::Version
173
+ version: 0.85.1
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - '='
179
+ - !ruby/object:Gem::Version
180
+ version: 0.85.1
167
181
  - !ruby/object:Gem::Dependency
168
182
  name: mongoid
169
183
  requirement: !ruby/object:Gem::Requirement
@@ -258,6 +272,7 @@ files:
258
272
  - lib/arkaan/concerns/activable.rb
259
273
  - lib/arkaan/concerns/diagnosticable.rb
260
274
  - lib/arkaan/concerns/enumerable.rb
275
+ - lib/arkaan/concerns/historizable.rb
261
276
  - lib/arkaan/concerns/mime_typable.rb
262
277
  - lib/arkaan/concerns/premiumable.rb
263
278
  - lib/arkaan/concerns/sluggable.rb
@@ -265,6 +280,7 @@ files:
265
280
  - lib/arkaan/decorators/errors.rb
266
281
  - lib/arkaan/decorators/errors/env_variable_missing.rb
267
282
  - lib/arkaan/decorators/gateway.rb
283
+ - lib/arkaan/event.rb
268
284
  - lib/arkaan/factories.rb
269
285
  - lib/arkaan/factories/errors.rb
270
286
  - lib/arkaan/factories/errors/gateway_not_found.rb