effective_resources 1.0.6 → 1.0.7

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: fc5c9f80574e6e2e358159bd7b4bd8805f788f80
4
- data.tar.gz: c673dbb171613323e3fd32cc3dfc4e9b4f67db2c
3
+ metadata.gz: 98eec49f950e3a5329e04edcdc12462a46511a64
4
+ data.tar.gz: 86cb3c947ff46aa3cad31418d7cd9fb0bbc0b9f0
5
5
  SHA512:
6
- metadata.gz: 5c66fbaeb3387368ce66f4f7784e8b04084a3ad417b89c8b9a72055c3fd9a59d59683a1b542e6c5cb703ac7064bfc6d7cea659fc12c2a35056b5002b2d0ea5d5
7
- data.tar.gz: d04261af330fdd6028405232143882eb82db90f8bdde0a080bd40e48318172dbb7273bf0b5b8ebf6179768a18b531aada8071cbde4ab9f8848c8e5d71f43f6da
6
+ metadata.gz: 79eaabdd4351233a3cb816127449a9c94b5ceab6d4e7e0267c631f8f4d232f7a6db174bfb3b618b7a9ceb824bb33aa59db7f2ca02ea763e38fa12c77e65aa12e
7
+ data.tar.gz: 806feda1c2f95426a79aafafac3adb353f506bb44d5660d6249b78b8fbde92282e5a22e8cdcd1396aca3cbb9d38fbab09a72c063407843e138bbb5a182b15220
@@ -11,8 +11,8 @@ module EffectiveResourcesPrivateHelper
11
11
 
12
12
  (args.key?(:only) ? args[:only].include?(page_action) : true) &&
13
13
  (args.key?(:except) ? !args[:except].include?(page_action) : true) &&
14
- (args.key?(:if) ? controller.instance_exec(&args[:if]) : true) &&
15
- (args.key?(:unless) ? !controller.instance_exec(&args[:unless]) : true) &&
14
+ (args.key?(:if) ? controller.instance_exec(resource, &args[:if]) : true) &&
15
+ (args.key?(:unless) ? !controller.instance_exec(resource, &args[:unless]) : true) &&
16
16
  EffectiveResources.authorized?(controller, action, resource)
17
17
  end.inject({}) do |h, (commit, args)|
18
18
  opts = args.except(:default, :only, :except, :if, :unless, :redirect, :success, :danger)
@@ -0,0 +1,99 @@
1
+ # ActsAsArchived
2
+ #
3
+ # Implements the dumb archived pattern
4
+ # An archived object should not be displayed on index screens, or any related resource's #new pages
5
+ # effective_select (from the effective_bootstrap gem) is aware of this concern, and calls .unarchived and .archived appropriately when passed an ActiveRecord relation
6
+ # Use the cascade argument to cascade archived changes to any has_manys
7
+ #
8
+ # class Thing < ApplicationRecord
9
+ # has_many :comments
10
+ # acts_as_archivable cascade: :comments
11
+ # end
12
+
13
+ # Each controller needs its own archive and unarchive action.
14
+ # To simplify this, use the following route concern.
15
+ #
16
+ # In your routes.rb:
17
+ #
18
+ # Rails.application.routes.draw do
19
+ # acts_as_archived
20
+ #
21
+ # resource :things, concern: :acts_as_archived
22
+ # resource :comments, concern: :acts_as_archived
23
+ # end
24
+ #
25
+ # and include Effective::CrudController in your resource controller
26
+
27
+ module ActsAsArchived
28
+ extend ActiveSupport::Concern
29
+
30
+ module ActiveRecord
31
+ def acts_as_archived(cascade: [])
32
+ resource = new()
33
+
34
+ # Make sure we respond to archived attribute
35
+ # puts "WARNING: (acts_as_archived) expected #{name} to respond to archived" unless resource.respond_to?(:archived)
36
+
37
+ # Parse options
38
+ cascade = Array(cascade).compact
39
+
40
+ if cascade.any? { |obj| !obj.kind_of?(Symbol) }
41
+ raise 'expected cascade to be an Array of has_many symbols'
42
+ end
43
+
44
+ cascade.reject { |cascade| cascade.respond_to?(cascade) }.each do |cascade|
45
+ puts "WARNING: (acts_as_archived) expected #{name} to respond to #{cascade}."
46
+ end
47
+
48
+ @acts_as_archived_options = { cascade: cascade }
49
+
50
+ include ::ActsAsArchived
51
+ end
52
+ end
53
+
54
+ module RoutesConcern
55
+ def acts_as_archived
56
+ concern :acts_as_archived do
57
+ post :archive, on: :member
58
+ post :unarchive, on: :member
59
+ end
60
+ end
61
+ end
62
+
63
+ included do
64
+ scope :archived, -> { where(archived: true) }
65
+ scope :unarchived, -> { where(archived: false) }
66
+
67
+ effective_resource do
68
+ archived :boolean, permitted: false
69
+ end
70
+
71
+ acts_as_archived_options = @acts_as_archived_options
72
+ self.send(:define_method, :acts_as_archived_options) { acts_as_archived_options }
73
+ end
74
+
75
+ module ClassMethods
76
+ def acts_as_archived?; true; end
77
+ end
78
+
79
+ # Instance methods
80
+ def archive!
81
+ transaction do
82
+ update!(archived: true) # Runs validations
83
+ acts_as_archived_options[:cascade].each { |obj| public_send(obj).update_all(archived: true) }
84
+ end
85
+ end
86
+
87
+ def unarchive!
88
+ transaction do
89
+ update_column(:archived, false) # Does not run validations
90
+ acts_as_archived_options[:cascade].each { |obj| public_send(obj).update_all(archived: false) }
91
+ end
92
+ end
93
+
94
+ def destroy
95
+ archive!
96
+ end
97
+
98
+ end
99
+
@@ -13,8 +13,6 @@ module Effective
13
13
  end
14
14
 
15
15
  def method_missing(m, *args, &block)
16
- raise "#{m} has already been defined" if attributes[m]
17
-
18
16
  if m == :timestamps
19
17
  attributes[:created_at] = [:datetime]
20
18
  attributes[:updated_at] = [:datetime]
@@ -13,11 +13,11 @@ module Effective
13
13
  end
14
14
 
15
15
  if actions.find { |a| a == :index }
16
- submits['Continue'] = { action: :save, redirect: :index, default: true, unless: -> { params[:_datatable_id] } }
16
+ submits['Continue'] = { action: :save, redirect: :index, default: true, unless: -> (resource) { params[:_datatable_id] } }
17
17
  end
18
18
 
19
19
  if actions.find { |a| a == :new }
20
- submits['Add New'] = { action: :save, redirect: :new, default: true, unless: -> { params[:_datatable_id] } }
20
+ submits['Add New'] = { action: :save, redirect: :new, default: true, unless: -> (resource) { params[:_datatable_id] } }
21
21
  end
22
22
  end
23
23
  end
@@ -29,11 +29,19 @@ module Effective
29
29
  end
30
30
 
31
31
  (member_post_actions - crud_actions).each do |action| # default true means it will be overwritten by dsl methods
32
- buttons[action.to_s.titleize] = { action: action, default: true, 'data-method' => :post, 'data-confirm' => "Really #{action} @reource?"}
32
+ buttons[action.to_s.titleize] = case action
33
+ when :archive
34
+ { action: action, default: true, if: -> (resource) { !resource.archived? }, class: 'btn btn-danger', 'data-method' => :post, 'data-confirm' => "Really #{action} @resource?"}
35
+ when :unarchive
36
+ { action: action, default: true, if: -> (resource) { resource.archived? }, 'data-method' => :post, 'data-confirm' => "Really #{action} @resource?" }
37
+ else
38
+ { action: action, default: true, 'data-method' => :post, 'data-confirm' => "Really #{action} @resource?"}
39
+ end
33
40
  end
34
41
 
35
42
  member_delete_actions.each do |action|
36
43
  if action == :destroy
44
+ next if buttons.values.find { |v| v[:action] == :archive }.present?
37
45
  buttons['Delete'] = { action: action, default: true, 'data-method' => :delete, 'data-confirm' => "Really delete @resource?" }
38
46
  else
39
47
  buttons[action.to_s.titleize] = { action: action, default: true, 'data-method' => :delete, 'data-confirm' => "Really #{action} @resource?" }
@@ -68,10 +76,20 @@ module Effective
68
76
 
69
77
  (member_post_actions - crud_actions).each do |action|
70
78
  actions[action.to_s.titleize] = { action: action, default: true, 'data-method' => :post, 'data-confirm' => "Really #{action} @resource?" }
79
+
80
+ actions[action.to_s.titleize] = case action
81
+ when :archive
82
+ { action: action, default: true, if: -> (resource) { !resource.archived? }, class: 'btn btn-danger', 'data-method' => :post, 'data-confirm' => "Really #{action} @resource?"}
83
+ when :unarchive
84
+ { action: action, default: true, if: -> (resource) { resource.archived? }, 'data-method' => :post, 'data-confirm' => "Really #{action} @resource?" }
85
+ else
86
+ { action: action, default: true, 'data-method' => :post, 'data-confirm' => "Really #{action} @resource?" }
87
+ end
71
88
  end
72
89
 
73
90
  member_delete_actions.each do |action|
74
91
  if action == :destroy
92
+ next if actions.values.find { |v| v[:action] == :archive }.present?
75
93
  actions['Delete'] = { action: action, default: true, 'data-method' => :delete, 'data-confirm' => "Really delete @resource?" }
76
94
  else
77
95
  actions[action.to_s.titleize] = { action: action, default: true, 'data-method' => :delete, 'data-confirm' => "Really #{action} @resource?" }
@@ -9,6 +9,12 @@ module EffectiveResources
9
9
  eval File.read("#{config.root}/config/effective_resources.rb")
10
10
  end
11
11
 
12
+ # Register the acts_as_archived routes concern
13
+ # resources :things, concerns: :acts_as_archived
14
+ initializer 'effective_resources.routes_concern' do |app|
15
+ ActionDispatch::Routing::Mapper.include(ActsAsArchived::RoutesConcern)
16
+ end
17
+
12
18
  # Register the flash_messages concern so that it can be called in ActionController
13
19
  initializer 'effective_resources.action_controller' do |app|
14
20
  ActiveSupport.on_load :action_controller do
@@ -19,6 +25,7 @@ module EffectiveResources
19
25
  # Include acts_as_addressable concern and allow any ActiveRecord object to call it
20
26
  initializer 'effective_resources.active_record' do |app|
21
27
  ActiveSupport.on_load :active_record do
28
+ ActiveRecord::Base.extend(ActsAsArchived::ActiveRecord)
22
29
  ActiveRecord::Base.extend(ActsAsTokened::ActiveRecord)
23
30
  ActiveRecord::Base.extend(ActsAsSlugged::ActiveRecord)
24
31
  ActiveRecord::Base.extend(EffectiveResource::ActiveRecord)
@@ -1,3 +1,3 @@
1
1
  module EffectiveResources
2
- VERSION = '1.0.6'.freeze
2
+ VERSION = '1.0.7'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: effective_resources
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.6
4
+ version: 1.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Code and Effect
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-11-02 00:00:00.000000000 Z
11
+ date: 2018-11-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -45,6 +45,7 @@ files:
45
45
  - app/controllers/concerns/effective/flash_messages.rb
46
46
  - app/helpers/effective_resources_helper.rb
47
47
  - app/helpers/effective_resources_private_helper.rb
48
+ - app/models/concerns/acts_as_archived.rb
48
49
  - app/models/concerns/acts_as_slugged.rb
49
50
  - app/models/concerns/acts_as_tokened.rb
50
51
  - app/models/concerns/effective_resource.rb