resource_quotable 0.2.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.
Files changed (66) hide show
  1. checksums.yaml +7 -0
  2. data/Changelog.md +17 -0
  3. data/MIT-LICENSE +20 -0
  4. data/Rakefile +18 -0
  5. data/Readme.md +206 -0
  6. data/app/assets/config/resource_quotable_manifest.js +1 -0
  7. data/app/assets/stylesheets/resource_quotable/application.css +15 -0
  8. data/app/controllers/resource_quotable/application_controller.rb +15 -0
  9. data/app/controllers/resource_quotable/quota_controller.rb +106 -0
  10. data/app/controllers/resource_quotable/quotum_trackers_controller.rb +23 -0
  11. data/app/models/resource_quotable/application_record.rb +7 -0
  12. data/app/models/resource_quotable/quotum.rb +46 -0
  13. data/app/models/resource_quotable/quotum_tracker.rb +60 -0
  14. data/app/services/resource_quotable/action_services/base.rb +23 -0
  15. data/app/services/resource_quotable/action_services/check.rb +19 -0
  16. data/app/services/resource_quotable/action_services/check_multiple.rb +23 -0
  17. data/app/services/resource_quotable/action_services/increment.rb +18 -0
  18. data/app/services/resource_quotable/action_services/increment_multiple.rb +22 -0
  19. data/app/services/resource_quotable/base.rb +24 -0
  20. data/app/services/resource_quotable/create.rb +29 -0
  21. data/app/services/resource_quotable/destroy.rb +14 -0
  22. data/app/services/resource_quotable/quotum_tracker_services/reset.rb +17 -0
  23. data/app/services/resource_quotable/reset/all.rb +16 -0
  24. data/app/services/resource_quotable/reset/any.rb +16 -0
  25. data/app/services/resource_quotable/reset/base.rb +22 -0
  26. data/app/services/resource_quotable/reset/daily.rb +16 -0
  27. data/app/services/resource_quotable/reset/monthly.rb +16 -0
  28. data/app/services/resource_quotable/reset/weekly.rb +16 -0
  29. data/app/services/resource_quotable/reset/yearly.rb +16 -0
  30. data/app/services/resource_quotable/update.rb +23 -0
  31. data/app/views/layouts/resource_quotable/application.html.erb +15 -0
  32. data/app/views/resource_quotable/quota/_edit.html.erb +11 -0
  33. data/app/views/resource_quotable/quota/_index.html.erb +43 -0
  34. data/app/views/resource_quotable/quota/_new.html.erb +34 -0
  35. data/app/views/resource_quotable/quota/_show.html.erb +41 -0
  36. data/app/views/resource_quotable/quota/create.js.erb +3 -0
  37. data/app/views/resource_quotable/quota/destroy.js.erb +3 -0
  38. data/app/views/resource_quotable/quota/edit.html.erb +1 -0
  39. data/app/views/resource_quotable/quota/edit.js.erb +1 -0
  40. data/app/views/resource_quotable/quota/index/_row.html.erb +3 -0
  41. data/app/views/resource_quotable/quota/index/_row_content.html.erb +43 -0
  42. data/app/views/resource_quotable/quota/index.html.erb +1 -0
  43. data/app/views/resource_quotable/quota/index.js.erb +1 -0
  44. data/app/views/resource_quotable/quota/new.html.erb +1 -0
  45. data/app/views/resource_quotable/quota/new.js.erb +1 -0
  46. data/app/views/resource_quotable/quota/show/_row.html.erb +3 -0
  47. data/app/views/resource_quotable/quota/show/_row_content.html.erb +7 -0
  48. data/app/views/resource_quotable/quota/show.html.erb +1 -0
  49. data/app/views/resource_quotable/quota/show.js.erb +1 -0
  50. data/app/views/resource_quotable/quota/update.js.erb +3 -0
  51. data/app/views/resource_quotable/quotum_trackers/index/_row_content.html.erb +8 -0
  52. data/app/views/resource_quotable/quotum_trackers/reset.js.erb +3 -0
  53. data/config/routes.rb +11 -0
  54. data/db/migrate/20220805102601_create_resource_quotable_quota.rb +17 -0
  55. data/db/migrate/20220811072023_create_resource_quotable_quotum_trackers.rb +18 -0
  56. data/lib/concerns/controllers/allowed_to_manage_quota_check.rb +73 -0
  57. data/lib/concerns/models/act_as_quota_trackable.rb +23 -0
  58. data/lib/concerns/models/act_as_quotable.rb +19 -0
  59. data/lib/resource_quotable/engine.rb +13 -0
  60. data/lib/resource_quotable/exeptions.rb +9 -0
  61. data/lib/resource_quotable/generators/views_generator.rb +18 -0
  62. data/lib/resource_quotable/helper.rb +27 -0
  63. data/lib/resource_quotable/version.rb +5 -0
  64. data/lib/resource_quotable.rb +68 -0
  65. data/lib/tasks/resource_quotable_tasks.rake +5 -0
  66. metadata +322 -0
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ResourceQuotable
4
+ # Create Quota and all quota_trackers for a group of users.
5
+ class Create < Base
6
+ attr_accessor :group, :action, :resource, :limit, :period
7
+
8
+ validates :group, :resource, presence: true
9
+ validates_inclusion_of :action, in: ResourceQuotable.actions.keys
10
+ validates_inclusion_of :period, in: Quotum.periods.keys.map(&:to_sym)
11
+ validates_numericality_of :limit, only_integer: true, greater_than_or_equal_to: 0
12
+
13
+ def call
14
+ # Crear Quotum
15
+ # Crear QuotaTracker user existentes.
16
+ quotum = nil
17
+ flag = !limit.positive?
18
+
19
+ ResourceQuotable::Quotum.transaction do
20
+ quotum = group.quota.create!(resource_class: resource, action: action, period: period, limit: limit)
21
+ group.resource_quotable_users.map { |user| user.quotum_trackers.create!(quotum: quotum, counter: 0, flag: flag) }
22
+ end
23
+
24
+ quotum
25
+ rescue ActiveRecord::RecordNotUnique
26
+ raise ResourceQuotable::QuotaDuplicateError
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ResourceQuotable
4
+ # Destroy Quota
5
+ class Destroy < Base
6
+ attr_accessor :quotum
7
+
8
+ validates :quotum, presence: true
9
+
10
+ def call
11
+ quotum.destroy
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ResourceQuotable
4
+ module QuotumTrackerServices
5
+ # Reset a quotum_tracker instance.
6
+ class Reset < ::ResourceQuotable::Base
7
+ attr_accessor :quotum_tracker
8
+
9
+ validates :quotum_tracker, presence: true
10
+
11
+ def call
12
+ quotum_tracker.reset!
13
+ quotum_tracker.reload
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ResourceQuotable
4
+ module Reset
5
+ # Reset Quota for all period.
6
+ class All < Base
7
+ include ActiveModel::Validations
8
+
9
+ protected
10
+
11
+ def quotum_in_period
12
+ Quotum.all
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ResourceQuotable
4
+ module Reset
5
+ # Reset Quota for Any period.
6
+ class Any < Base
7
+ include ActiveModel::Validations
8
+
9
+ protected
10
+
11
+ def quotum_in_period
12
+ Quotum.any_period
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ResourceQuotable
4
+ module Reset
5
+ # Base Reset Quota for X period.
6
+ class Base < ResourceQuotable::Base
7
+ include ActiveModel::Validations
8
+
9
+ def call
10
+ quotum_in_period.each do |quotum|
11
+ quotum.quotum_trackers.with_active_counter.map(&:reset!)
12
+ end
13
+ end
14
+
15
+ protected
16
+
17
+ def quotum_in_period
18
+ raise ResourceQuotable::AbstractClassError
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ResourceQuotable
4
+ module Reset
5
+ # Reset Quota for daily period.
6
+ class Daily < Base
7
+ include ActiveModel::Validations
8
+
9
+ protected
10
+
11
+ def quotum_in_period
12
+ Quotum.daily_period
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ResourceQuotable
4
+ module Reset
5
+ # Reset Quota for Monthly period.
6
+ class Monthly < Base
7
+ include ActiveModel::Validations
8
+
9
+ protected
10
+
11
+ def quotum_in_period
12
+ Quotum.monthly_period
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ResourceQuotable
4
+ module Reset
5
+ # Reset Quota for Weekly period.
6
+ class Weekly < Base
7
+ include ActiveModel::Validations
8
+
9
+ protected
10
+
11
+ def quotum_in_period
12
+ Quotum.weekly_period
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ResourceQuotable
4
+ module Reset
5
+ # Reset Quota for Yearly period.
6
+ class Yearly < Base
7
+ include ActiveModel::Validations
8
+
9
+ protected
10
+
11
+ def quotum_in_period
12
+ Quotum.yearly_period
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ResourceQuotable
4
+ # Update Quota and reset QuotumTracker flag according
5
+ class Update < Base
6
+ attr_accessor :quotum, :limit
7
+
8
+ validates :quotum, presence: true
9
+ validates_numericality_of :limit, only_integer: true, greater_than_or_equal_to: 0
10
+
11
+ def call
12
+ quotum.update(limit: limit)
13
+
14
+ # rubocop:disable Rails/SkipsModelValidations
15
+ # We know what we are doing. Validation and callback are not needed here.
16
+ quotum.quotum_trackers.under_limit(limit).flagged.update_all(flag: false)
17
+ quotum.quotum_trackers.over_limit(limit).not_flagged.update_all(flag: true)
18
+ # rubocop:enable Rails/SkipsModelValidations
19
+
20
+ quotum
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,15 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Resource quotable</title>
5
+ <%= csrf_meta_tags %>
6
+ <%= csp_meta_tag %>
7
+
8
+ <%= stylesheet_link_tag "resource_quotable/application", media: "all" %>
9
+ </head>
10
+ <body>
11
+
12
+ <%= yield %>
13
+
14
+ </body>
15
+ </html>
@@ -0,0 +1,11 @@
1
+ <%= form_for @quotum, method: :put, remote: true do |f| %>
2
+ <fieldset>
3
+ <div class="form-group col-md-12">
4
+ <label class="form-control-label">Limit</label>
5
+ <%= f.number_field :limit, class: 'form-control', required: true %>
6
+ </div>
7
+ <div class="form-group col-md-12">
8
+ <%= f.submit 'Save', class: 'btn btn-success' %>
9
+ </div>
10
+ </fieldset>
11
+ <% end %>
@@ -0,0 +1,43 @@
1
+ <div class="row">
2
+ <div class="col-md-12">
3
+ <div class="panel panel-inverse lobiable lobi_scrollable" data-unpin=false data-minimize=false data-close=false data-edit-title=false data-reload=false data-edit=false data-fullscreen=true>
4
+ <div class="panel-heading">
5
+ <div class="panel-title" style="max-width: calc(100% - 120px);">
6
+ <h4>Quotas</h4>
7
+ </div>
8
+ </div>
9
+
10
+ <div class="panel-body" >
11
+ <% if @quota.empty? %>
12
+ <div class='row'>
13
+ <div class='col-md-12 text-center'>
14
+ <h3>No results</h3>
15
+ </div>
16
+ </div>
17
+ <% else %>
18
+ <div class='row'>
19
+ <div class='col-md-12'>
20
+ <table id='resource_quotable_table' class="table table-striped medium text-left">
21
+ <tbody>
22
+ <tr>
23
+ <th>Id</th>
24
+ <th>User</th>
25
+ <th>Resource</th>
26
+ <th>Action</th>
27
+ <th>Period</th>
28
+ <th>Limit</th>
29
+ <th></th>
30
+ </tr>
31
+ <% @quota.each do |quotum| %>
32
+ <%= render partial: 'resource_quotable/quota/index/row', locals: { quotum: quotum } %>
33
+ <% end %>
34
+ </tbody>
35
+ </table>
36
+ <%= paginate @quota, remote: true %>
37
+ </div>
38
+ </div>
39
+ <% end %>
40
+ </div>
41
+ </div>
42
+ </div>
43
+ </div>
@@ -0,0 +1,34 @@
1
+ <%= form_for @quotum_limit, remote: true do |f| %>
2
+ <fieldset>
3
+ <%= f.fields_for :quotum do |quotum_form| %>
4
+ <div class="form-group col-md-12">
5
+ <label class="form-control-label">User</label>
6
+ <%= quotum_form.select :user_id, options_from_collection_for_select(ResourceQuotable.user_class.all, :id, :name), { prompt: 'Select' }, class: "form-control", required: true %>
7
+ </div>
8
+
9
+ <div class="form-group col-md-12">
10
+ <label class="form-control-label">Resource</label>
11
+ <%= quotum_form.select :resource_class, options_for_select(ResourceQuotable.resources, @quotum_limit.quotum.resource_class), { prompt: 'Select' }, class: "form-control", required: true %>
12
+ </div>
13
+
14
+ <div class="form-group col-md-12">
15
+ <label class="form-control-label">Action</label>
16
+ <%= quotum_form.select :action, options_for_select(ResourceQuotable.actions.keys, @quotum_limit.quotum.action.to_s), { prompt: 'Select' }, class: "form-control", required: true %>
17
+ </div>
18
+ <% end %>
19
+
20
+ <div class="form-group col-md-12">
21
+ <label class="form-control-label">Period</label>
22
+ <%= f.select :period, options_for_select(ResourceQuotable::QuotumLimit.periods.keys, @quotum_limit.period.to_s), { prompt: 'Select' }, class: "form-control", required: true %>
23
+ </div>
24
+
25
+ <div class="form-group col-md-12">
26
+ <label class="form-control-label">Limit</label>
27
+ <%= f.number_field :limit, class: 'form-control', required: true %>
28
+ </div>
29
+
30
+ <div class="form-group col-md-12">
31
+ <%= f.submit 'Save', class: 'btn btn-success' %>
32
+ </div>
33
+ </fieldset>
34
+ <% end %>
@@ -0,0 +1,41 @@
1
+ <div class="row">
2
+ <div class="col-md-12">
3
+ <div class="panel panel-inverse lobiable lobi_scrollable" data-unpin=false data-minimize=false data-close=false data-edit-title=false data-reload=false data-edit=false data-fullscreen=true>
4
+ <div class="panel-heading">
5
+ <div class="panel-title" style="max-width: calc(100% - 120px);">
6
+ <h4>Quota Details for <%= "#{@quotum.action} #{@quotum.resource_class}. Period: #{@quotum.period}" %></h4>
7
+ </div>
8
+ </div>
9
+
10
+ <div class="panel-body" >
11
+ <% if @quotum_trackers.empty? %>
12
+ <div class='row'>
13
+ <div class='col-md-12 text-center'>
14
+ <h3>No results</h3>
15
+ </div>
16
+ </div>
17
+ <% else %>
18
+ <div class='row'>
19
+ <div class='col-md-12'>
20
+ <table id='resource_quotable_table' class="table table-striped medium text-left">
21
+ <tbody>
22
+ <tr>
23
+ <th>Id</th>
24
+ <th>User</th>
25
+ <th>Counter</th>
26
+ <th>Flag</th>
27
+ <th></th>
28
+ </tr>
29
+ <% @quotum_trackers.each do |quotum_tracker| %>
30
+ <%= render partial: 'resource_quotable/quota/show/row', locals: { quotum_tracker: quotum_tracker } %>
31
+ <% end %>
32
+ </tbody>
33
+ </table>
34
+ <%= paginate @quotum_trackers, remote: true %>
35
+ </div>
36
+ </div>
37
+ <% end %>
38
+ </div>
39
+ </div>
40
+ </div>
41
+ </div>
@@ -0,0 +1,3 @@
1
+ $('#resource_quotable_table').append("<%= escape_javascript render partial: 'index/row', locals: { quotum: @quotum } %>");
2
+
3
+ alert("Quotum Limit #<%= @quotum.id %> created");
@@ -0,0 +1,3 @@
1
+ $("#quotum_<%= @id %>").remove();
2
+
3
+ alert("Quotum Limit #<%= @id %> deleted");
@@ -0,0 +1 @@
1
+ <%= render partial: 'edit' %>
@@ -0,0 +1 @@
1
+ $("#<%= ResourceQuotable.main_content %>").html("<%= escape_javascript render partial: 'edit' %>")
@@ -0,0 +1,3 @@
1
+ <tr id="quotum_<%= quotum.id %>">
2
+ <%= render partial: 'resource_quotable/quota/index/row_content', locals: { quotum: quotum } %>
3
+ </tr>
@@ -0,0 +1,43 @@
1
+ <td><%= link_to quotum.id, quotum_path(quotum), remote: true %></td>
2
+ <td><%= quotum.user.name %></td>
3
+ <td><%= quotum.resource_class %></td>
4
+ <td><%= quotum.action %></td>
5
+ <td><%= quotum.period %></td>
6
+ <td><%= quotum.limit %></td>
7
+
8
+ <td class='text-right'>
9
+ <%= link_to '',
10
+ reset_quotum_path(quotum),
11
+ remote: true,
12
+ method: :put,
13
+ data: {
14
+ confirm: 'Are you sure you want to reset this quotum?',
15
+ tooltip: 'Reset quotum',
16
+ toggle: 'tooltip',
17
+ title: 'Reset quotum',
18
+ placement: 'bottom'
19
+ },
20
+ class: "fa fa-reload" %>
21
+ <%= link_to '',
22
+ edit_quotum_path(quotum),
23
+ remote: true,
24
+ class: 'fa fa-edit',
25
+ data: {
26
+ tooltip: 'Edit',
27
+ title: 'Edit',
28
+ toggle:'tooltip',
29
+ placement: 'left'
30
+ } %>
31
+ <%= link_to '',
32
+ quotum_path(quotum),
33
+ remote: true,
34
+ method: :delete,
35
+ data: {
36
+ confirm: 'Are you sure you want to destroy this quotum?',
37
+ tooltip: 'Delete',
38
+ toggle: 'tooltip',
39
+ title: 'Delete',
40
+ placement: 'left'
41
+ },
42
+ class: 'fa fa-trash' %>
43
+ </td>
@@ -0,0 +1 @@
1
+ <%= render partial: 'index' %>
@@ -0,0 +1 @@
1
+ $("#<%= ResourceQuotable.main_content %>").html("<%= escape_javascript render partial: 'index' %>")
@@ -0,0 +1 @@
1
+ <%= render partial: 'new' %>
@@ -0,0 +1 @@
1
+ $("#<%= ResourceQuotable.main_content %>").html("<%= escape_javascript render partial: 'new' %>")
@@ -0,0 +1,3 @@
1
+ <tr id="quotum_tracker_<%= quotum_tracker.id %>">
2
+ <%= render partial: 'resource_quotable/quota/show/row_content', locals: { quotum_tracker: quotum_tracker } %>
3
+ </tr>
@@ -0,0 +1,7 @@
1
+ <td><%= quotum_tracker.id %></td>
2
+ <td><%= link_to quotum_tracker.user.full_name, sp_app_user_path(quotum_tracker.user), remote: true, class: 'leadser_js_link_disable leadser_ujs_partial', data: { 'partial-spinner_div': '#leadser_page_content', 'url-push': true } %>
3
+ <td><%= quotum_tracker.counter %>/<%= quotum_tracker.limit %></td>
4
+ <td><%= quotum_tracker.flag %></td>
5
+
6
+ <td class='text-right'>
7
+ </td>
@@ -0,0 +1 @@
1
+ <%= render partial: 'show' %>
@@ -0,0 +1 @@
1
+ $("#<%= ResourceQuotable.main_content %>").html("<%= escape_javascript render partial: 'show' %>")
@@ -0,0 +1,3 @@
1
+ $("#quotum_<%= @quotum.id %>").replaceWith( "<%= escape_javascript render partial: 'index/row_content', locals: { quotum: @quotum } %>" );
2
+
3
+ alert("Quotum Limit #<%= @quotum.id %> updated");
@@ -0,0 +1,8 @@
1
+ <td><%= quotum_tracker.resource_class %></td>
2
+ <td><%= quotum_tracker.action %>
3
+ <td><%= quotum_tracker.quotum.period %>
4
+ <td><%= quotum_tracker.counter %>/<%= quotum_tracker.limit %></td>
5
+ <td><%= quotum_tracker.flag %></td>
6
+
7
+ <td class='text-right'>
8
+ </td>
@@ -0,0 +1,3 @@
1
+ $("#quotum_tracker_<%= @quotum_tracker.id %>").replaceWith( "<%= escape_javascript render partial: 'index/row_content', locals: { quotum_tracker: @quotum_tracker } %>" );
2
+
3
+ alert("Quotum Tracker #<%= @quotum_tracker.id %> updated");
data/config/routes.rb ADDED
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ ResourceQuotable::Engine.routes.draw do
4
+ resources :quota
5
+
6
+ resources :quotum_trackers, only: [] do
7
+ put :reset, on: :member
8
+ end
9
+
10
+ root 'quota#index'
11
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ class CreateResourceQuotableQuota < ActiveRecord::Migration[4.2] # :nodoc:
4
+ def change
5
+ create_table :resource_quotable_quota do |t|
6
+ t.integer :group_id, null: false, index: true
7
+ t.string :group_type, null: false
8
+ t.string :resource_class, null: false
9
+ t.integer :action, null: false, default: 0
10
+ t.integer :period, null: false, default: 0
11
+ t.integer :limit, null: false, default: 1
12
+
13
+ t.timestamps
14
+ end
15
+ add_index :resource_quotable_quota, %i[group_id resource_class action period], unique: true, name: 'resource_quotable_quota_unique_index'
16
+ end
17
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ # rubocop:disable Rails/CreateTableWithTimestamps
4
+ # No needs for timestamp here.
5
+ class CreateResourceQuotableQuotumTrackers < ActiveRecord::Migration[4.2] # :nodoc:
6
+ def change
7
+ create_table :resource_quotable_quotum_trackers do |t|
8
+ t.integer :quotum_id, null: false, index: true
9
+ t.integer :user_id, null: false, index: true
10
+ t.string :user_type, null: false
11
+ t.boolean :flag, null: false, default: false
12
+ t.integer :counter, null: false, default: 0
13
+ end
14
+ add_index :resource_quotable_quotum_trackers, %i[user_id quotum_id], unique: true, name: 'resource_quotable_quotum_trackers_unique_index'
15
+ end
16
+ end
17
+
18
+ # rubocop:enable Rails/CreateTableWithTimestamps
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ResourceQuotable
4
+ # API inclusion to ApplicationController in order to allow customizations.
5
+ #
6
+ module AllowedToManageQuotaCheck
7
+ extend ActiveSupport::Concern
8
+
9
+ included do
10
+ helper_method :allowed_to?
11
+
12
+ def allowed_to?(action, resource)
13
+ !ResourceQuotable::ActionServices::Check.call(
14
+ user: load_quotable_tracker_user,
15
+ resource: resource,
16
+ action: action
17
+ )
18
+ end
19
+
20
+ def allowed_to_do_multi?(action, resource, amount)
21
+ !ResourceQuotable::ActionServices::CheckMultiple.call(
22
+ user: load_quotable_tracker_user,
23
+ resource: resource,
24
+ action: action,
25
+ amount: amount
26
+ )
27
+ end
28
+
29
+ def quota_authorize!(action, resource)
30
+ raise ResourceQuotable::QuotaLimitError unless allowed_to?(action, resource)
31
+ end
32
+
33
+ def quota_authorize_multiple!(action, resource, amount)
34
+ raise ResourceQuotable::QuotaMultiLimitError unless allowed_to_do_multi?(action, resource, amount)
35
+ end
36
+
37
+ def quota_increment!(action, resource)
38
+ quota_authorize!(action, resource)
39
+ ResourceQuotable::ActionServices::Increment.call(
40
+ user: load_quotable_tracker_user,
41
+ resource: resource,
42
+ action: action
43
+ )
44
+ end
45
+
46
+ def quota_increment_multiple!(action, resource, amount)
47
+ quota_authorize_multiple!(action, resource, amount)
48
+ ResourceQuotable::ActionServices::IncrementMultiple.call(
49
+ user: load_quotable_tracker_user,
50
+ resource: resource,
51
+ action: action,
52
+ amount: amount
53
+ )
54
+ end
55
+
56
+ def allowed_to_manage_quota?
57
+ true
58
+ end
59
+
60
+ def quota_scoped
61
+ Quotum
62
+ end
63
+
64
+ def load_quotable_tracker_user
65
+ current_user
66
+ end
67
+
68
+ def load_quotable_group; end
69
+ def resource_quotable_before; end
70
+ def resource_quotable_after; end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ResourceQuotable
4
+ module ActsAsQuotaTrackable # :nodoc:
5
+ extend ActiveSupport::Concern
6
+
7
+ class_methods do
8
+ def acts_as_quota_trackable(_options = {})
9
+ has_many :quotum_trackers,
10
+ dependent: :destroy,
11
+ class_name: 'ResourceQuotable::QuotumTracker',
12
+ foreign_key: 'user_id',
13
+ inverse_of: :user
14
+
15
+ define_method(:resource_quotable_group) { send(ResourceQuotable.group_method) }
16
+
17
+ define_method(:quota_for_resource_action) do |resource, action|
18
+ resource_quotable_group.quota.for_resource_action(resource, action)
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ResourceQuotable
4
+ module ActsAsQuotable # :nodoc:
5
+ extend ActiveSupport::Concern
6
+
7
+ class_methods do
8
+ def acts_as_quotable(_options = {})
9
+ has_many :quota,
10
+ dependent: :destroy,
11
+ class_name: 'ResourceQuotable::Quotum',
12
+ foreign_key: 'group_id',
13
+ inverse_of: :group
14
+
15
+ define_method(:resource_quotable_users) { send(ResourceQuotable.users_method) }
16
+ end
17
+ end
18
+ end
19
+ end