effective_mergery 0.1.3 → 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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: fa0266aea59888bb91fe9f432df263bd3a4d59da
4
- data.tar.gz: 5c3e98a63f9652d832f05740a4881e3b6d7424bf
2
+ SHA256:
3
+ metadata.gz: '09d729cc7ba2f40740a83f58b09c7244d2f82f323b7b50fd9ac32f43cbd0b009'
4
+ data.tar.gz: ee2e412bf88531636840f41817c5b366516f6fa7c1d7abbeca9ef2eb20145bdf
5
5
  SHA512:
6
- metadata.gz: 497e5931828591453996d91606135b3aa9836819da5c92256a8196fb29234fc1d9c7079f2f38215f0f5128b6d4d9b00ad3c6482e21b1b8983544ec31f9222038
7
- data.tar.gz: 89900d334b0d29f4199c5e09d72c0a6678d2c835ebc20ccd170b9f0eb5aadb704cdbb8319776c5ab30545738ae8fecf4285ed2a08a6ebfef628cd14a819ab3ed
6
+ metadata.gz: e7ae5bfc66839df22f1b87fbf6533ae3e8c69aaa1895222c1ee6547222a108c37409c95508e488942470ec3e9f9c3ebb3b972469b4ae31dad749f29bba5e929f
7
+ data.tar.gz: 1cbe65cefab6cdb88dd5565a38c5eda4cdd6b9d0ee1221721b3d9a6291a292dec45948a43d72ff6badf5c6536b10208840975fab33c811bab7199a421620fcbe
data/MIT-LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright 2017 Code and Effect Inc.
1
+ Copyright 2023 Code and Effect Inc.
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
@@ -0,0 +1,2 @@
1
+ //= link_directory ../javascripts .js
2
+ //= link_directory ../stylesheets .css
@@ -1,17 +1,20 @@
1
1
  module Admin
2
2
  class MergeController < ApplicationController
3
- before_action :authenticate_user! if defined?(Devise)
3
+ before_action(:authenticate_user!) if defined?(Devise)
4
+ before_action { EffectiveResources.authorize!(self, :admin, :effective_mergery) }
4
5
 
5
- layout (EffectiveMergery.layout.kind_of?(Hash) ? EffectiveMergery.layout[:admin_merge] : EffectiveMergery.layout)
6
+ include Effective::CrudController
7
+
8
+ if (config = EffectiveMergery.layout)
9
+ layout(config.kind_of?(Hash) ? config[:admin] : config)
10
+ end
6
11
 
7
12
  def index
8
13
  @page_title = 'Merges'
9
- EffectiveMergery.authorized?(self, :admin, :effective_mergery)
10
14
  end
11
15
 
12
16
  def new
13
17
  @page_title = 'New Merge'
14
- EffectiveMergery.authorized?(self, :admin, :effective_mergery)
15
18
 
16
19
  begin
17
20
  @merge = Effective::Merge.new(type: params[:type])
@@ -23,8 +26,6 @@ module Admin
23
26
  end
24
27
 
25
28
  def create
26
- EffectiveMergery.authorized?(self, :admin, :effective_mergery)
27
-
28
29
  @merge = Effective::Merge.new(merge_params)
29
30
 
30
31
  if @merge.save
@@ -55,8 +56,6 @@ module Admin
55
56
 
56
57
  # This is the AJAX request for the object's attributes
57
58
  def attributes
58
- EffectiveMergery.authorized?(self, :admin, :effective_mergery)
59
-
60
59
  object = Effective::Merge.new(type: params[:type]).collection.find(params[:id])
61
60
 
62
61
  if object.present?
@@ -69,7 +68,7 @@ module Admin
69
68
  private
70
69
 
71
70
  def merge_params
72
- params.require(:effective_merge).permit(:type, :source_id, :target_id)
71
+ params.require(:effective_merge).permit!
73
72
  end
74
73
 
75
74
  end
@@ -7,7 +7,7 @@ module Effective
7
7
  validate(if: -> { source_id.present? }) { @source ||= collection.find_by_id(source_id) }
8
8
  validate(if: -> { target_id.present? }) { @target ||= collection.find_by_id(target_id) }
9
9
 
10
- validates :type, presence: true, inclusion: { in: EffectiveMergery.mergables, message: 'must be a mergable type' }
10
+ validates :type, presence: true
11
11
  validates :source_id, presence: true, unless: -> { source.present? }
12
12
  validates :target_id, presence: true, unless: -> { target.present? }
13
13
 
@@ -48,7 +48,7 @@ module Effective
48
48
  # This is called on Admin::Merges#new
49
49
  def validate_klass!
50
50
  raise "type can't be blank" unless type.present?
51
- raise 'type must be a mergable type' unless EffectiveMergery.mergables.include?(type)
51
+ raise 'type must be a mergable type' unless EffectiveMergery.mergables.map(&:name).include?(type)
52
52
  raise "invalid ActiveRecord klass" unless klass
53
53
  raise "invalid ActiveRecord collection" unless collection.kind_of?(ActiveRecord::Relation)
54
54
  true
@@ -58,37 +58,26 @@ module Effective
58
58
 
59
59
  def merge!(validate: true)
60
60
  resource = Effective::Resource.new(source)
61
+ success = false
61
62
 
62
63
  klass.transaction do
63
- begin
64
+ # Merge associations
65
+ (resource.has_ones + resource.has_manys + resource.nested_resources).compact.each do |association|
66
+ next if association.options[:through].present?
64
67
 
65
- # Merge associations
66
- (resource.has_ones + resource.has_manys + resource.nested_resources).compact.each do |association|
67
- next if association.options[:through].present?
68
-
69
- Array(source.send(association.name)).each do |obj|
70
- obj.assign_attributes(association.foreign_key => target.id)
71
-
72
- unless obj.save(validate: validate)
73
- raise "associated #{obj.class.name}<#{obj.to_param}> is invalid: #{obj.errors.full_messages.to_sentence}"
74
- end
75
- end
68
+ Array(source.send(association.name)).each do |obj|
69
+ obj.assign_attributes(association.foreign_key => target.id)
70
+ obj.save!(validate: validate)
76
71
  end
72
+ end
77
73
 
78
- source.destroy!
79
-
80
- unless target.save(validate: validate)
81
- raise "merged #{target.class.name} is invalid: #{target.errors.full_messages.to_sentence}"
82
- end
74
+ source.destroy!
83
75
 
84
- return true
85
- rescue => e
86
- self.errors.add(:base, e.message)
87
- raise ActiveRecord::Rollback
88
- end
76
+ target.save!(validate: validate)
77
+ success = true
89
78
  end
90
79
 
91
- false
80
+ success
92
81
  end
93
82
 
94
83
  end
@@ -3,7 +3,7 @@
3
3
  %p Please select a source and target. The source record's associated data, but not its attributes, will be merged into the target. The source record will then be destroyed.
4
4
 
5
5
  .effective-merge
6
- = simple_form_for [:admin, @merge], (EffectiveOrders.admin_simple_form_options || {}).merge(url: effective_mergery.admin_merge_index_path) do |f|
6
+ = simple_form_for([:admin, @merge], url: effective_mergery.admin_merge_index_path) do |f|
7
7
  = f.input :type, as: :hidden
8
8
 
9
9
  .row
@@ -1,26 +1,6 @@
1
1
  EffectiveMergery.setup do |config|
2
- # Authorization Method
3
- #
4
- # This method is called by all controller actions with the appropriate action and resource
5
- # If the method returns false, an Effective::AccessDenied Error will be raised (see README.md for complete info)
6
- #
7
- # Use via Proc (and with CanCan):
8
- # config.authorization_method = Proc.new { |controller, action, resource| can?(action, resource) }
9
- #
10
- # Use via custom method:
11
- # config.authorization_method = :my_authorization_method
12
- #
13
- # And then in your application_controller.rb:
14
- #
15
- # def my_authorization_method(action, resource)
16
- # current_user.is?(:admin)
17
- # end
18
- #
19
- # Or disable the check completely:
20
- # config.authorization_method = false
21
- config.authorization_method = Proc.new { |controller, action, resource| authorize!(action, resource) } # CanCanCan
22
2
 
23
- # Admin Screens Layout Settings
3
+ # Admin Screens Layout Settings
24
4
  config.layout = 'application' # All EffectiveMergery controllers will use this layout
25
5
 
26
6
  # config.layout = {
@@ -39,10 +19,6 @@ EffectiveMergery.setup do |config|
39
19
  # }
40
20
  # }
41
21
 
42
- # Allow merges for only the following ActiveRecord class names:
43
- # config.only = ['User']
44
-
45
- # Allow merges on all ActiveRecord classes, except the following:
46
- # config.except = []
47
-
22
+ # The class names that can be merged
23
+ # config.class_names = ['User']
48
24
  end
@@ -2,8 +2,6 @@ module EffectiveMergery
2
2
  class Engine < ::Rails::Engine
3
3
  engine_name 'effective_mergery'
4
4
 
5
- config.autoload_paths += Dir["#{config.root}/lib/"]
6
-
7
5
  # Set up our default configuration options.
8
6
  initializer "effective_mergery.defaults", before: :load_config_initializers do |app|
9
7
  eval File.read("#{config.root}/config/effective_mergery.rb")
@@ -1,3 +1,3 @@
1
1
  module EffectiveMergery
2
- VERSION = '0.1.3'.freeze
2
+ VERSION = '0.2.0'.freeze
3
3
  end
@@ -4,41 +4,15 @@ require 'effective_mergery/version'
4
4
 
5
5
  module EffectiveMergery
6
6
 
7
- # The following are all valid config keys
8
- mattr_accessor :authorization_method
9
- mattr_accessor :layout
10
- mattr_accessor :admin_simple_form_options
11
-
12
- mattr_accessor :only
13
- mattr_accessor :except
14
-
15
- def self.setup
16
- yield self
7
+ def self.config_keys
8
+ [:layout, :admin_simple_form_options, :class_names]
17
9
  end
18
10
 
19
- def self.authorized?(controller, action, resource)
20
- if authorization_method.respond_to?(:call) || authorization_method.kind_of?(Symbol)
21
- raise Effective::AccessDenied.new() unless (controller || self).instance_exec(controller, action, resource, &authorization_method)
22
- end
23
- true
24
- end
11
+ include EffectiveGem
25
12
 
13
+ # Just consider the onlies right now. sorry future matt.
26
14
  def self.mergables
27
- @mergables ||= (
28
- Rails.application.eager_load! unless Rails.configuration.cache_classes
29
-
30
- blacklist = ['Delayed::', 'Effective::', 'ActiveRecord::', 'ApplicationRecord']
31
-
32
- ActiveRecord::Base.descendants.map { |obj| obj.name }.tap do |names|
33
- names.reject! { |name| blacklist.any? { |b| name.start_with?(b) } }
34
-
35
- if (onlies = Array(only)).present?
36
- names.select! { |name| onlies.include?(name) }
37
- elsif (excepts = Array(except)).present?
38
- names.reject! { |name| excepts.include?(name) }
39
- end
40
- end.sort
41
- )
15
+ Array(class_names).map { |name| name.constantize }
42
16
  end
43
17
 
44
18
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: effective_mergery
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Code and Effect
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-07-11 00:00:00.000000000 Z
11
+ date: 2023-05-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -53,7 +53,7 @@ dependencies:
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
- name: sass-rails
56
+ name: sassc
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - ">="
@@ -89,12 +89,12 @@ extra_rdoc_files: []
89
89
  files:
90
90
  - MIT-LICENSE
91
91
  - README.md
92
+ - app/assets/config/effective_mergery_manifest.js
92
93
  - app/assets/javascripts/effective_mergery.js
93
94
  - app/assets/javascripts/effective_mergery/merge.js.coffee
94
95
  - app/assets/stylesheets/effective_mergery.scss
95
96
  - app/assets/stylesheets/effective_mergery/_merge.scss
96
97
  - app/controllers/admin/merge_controller.rb
97
- - app/models/effective/access_denied.rb
98
98
  - app/models/effective/merge.rb
99
99
  - app/views/admin/merge/_attributes.html.haml
100
100
  - app/views/admin/merge/create.html.haml
@@ -110,7 +110,7 @@ homepage: https://github.com/code-and-effect/effective_mergery
110
110
  licenses:
111
111
  - MIT
112
112
  metadata: {}
113
- post_install_message:
113
+ post_install_message:
114
114
  rdoc_options: []
115
115
  require_paths:
116
116
  - lib
@@ -125,9 +125,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
125
125
  - !ruby/object:Gem::Version
126
126
  version: '0'
127
127
  requirements: []
128
- rubyforge_project:
129
- rubygems_version: 2.4.5.1
130
- signing_key:
128
+ rubygems_version: 3.3.7
129
+ signing_key:
131
130
  specification_version: 4
132
131
  summary: Deep merge any two Active Record objects.
133
132
  test_files: []
@@ -1,17 +0,0 @@
1
- unless defined?(Effective::AccessDenied)
2
- module Effective
3
- class AccessDenied < StandardError
4
- attr_reader :action, :subject
5
-
6
- def initialize(message = nil, action = nil, subject = nil)
7
- @message = message
8
- @action = action
9
- @subject = subject
10
- end
11
-
12
- def to_s
13
- @message || I18n.t(:'unauthorized.default', :default => 'Access Denied')
14
- end
15
- end
16
- end
17
- end