effective_resources 0.3.5 → 0.3.6

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
  SHA1:
3
- metadata.gz: b018fe0f5e92d239f3139ac0eb959167099ee33f
4
- data.tar.gz: 2459f02e66bfabf218e2aa532d18fae10fdf910f
3
+ metadata.gz: 1c0486c174dd667b0157db3c09422b990ae5145d
4
+ data.tar.gz: 6c066a0c0ad0b9bfeabece6aad26b005db39a269
5
5
  SHA512:
6
- metadata.gz: 75db72dc613e30bcb488620e2dc15d900ae8dd8cabc6dca51a3ec100d55385e339c353667438a7bb03d9e1518837eb3b9038d9c01fa22cecb6734087cb22fe3e
7
- data.tar.gz: ea6d9ff7eb2a2c63dc7a84462387d490564e13038db5a6984a23ce6d75be06f577cdf7bb7bfaa1bd45b90fc2320ddefcb1db14cbde009a861a7620403569d079
6
+ metadata.gz: fb3b59b6125d11e74dc7699b6b11f78c7e5731bee9c0b8fc86380317acca67284773d075caa0a6978f184b3adec73063293883665693667516edf9a303e4e327
7
+ data.tar.gz: af797ee70b12863d379b7a7e7a404353791bf1b76d08aa4e3dff22a80ba9839b8d27cc6ef75ad5fa8dfb372fb4aa9e9321f288baaac47258466f78dcf278e741
data/README.md CHANGED
@@ -36,8 +36,15 @@ class PostsController < ApplicationController
36
36
 
37
37
  protected
38
38
 
39
+ # If it's a Hash of attributes, the controller will call .where(attributes)
40
+ # and the attributes will be used to initialize the datatable on index
41
+ #
42
+ # If it's an ActiveRecord scope, or symbol, we initialize a datatable with {resource_scope: true}
43
+ # and leave it as a TODO for the datatable to do the right thin .
39
44
  def post_scope
40
- {client_id: current_user.client_id} # Or a symbol
45
+ {client_id: current_user.client_id}
46
+ # Post.where(client_id: current_user.client_id)
47
+ # :approved
41
48
  end
42
49
 
43
50
  def post_params
@@ -12,10 +12,42 @@ module Effective
12
12
  define_method(action) do
13
13
  self.resource ||= resource_class.find(params[:id])
14
14
 
15
- @page_title ||= resource.to_s
16
15
  EffectiveResources.authorized?(self, action, resource)
16
+
17
+ if (request.post? || request.patch? || request.put?)
18
+ raise "expected @#{resource_name} to respond to #{action}!" unless resource.respond_to?("#{action}!")
19
+
20
+ begin
21
+ raise 'exception' unless resource.send("#{action}!")
22
+
23
+ flash[:success] = "Successfully #{action}#{action.to_s.end_with?('e') ? 'd' : 'ed'} #{resource_human_name}"
24
+ redirect_back fallback_location: resource_redirect_path
25
+ rescue => e
26
+ flash.now[:danger] = "Unable to #{action} #{resource_human_name}: #{resource.errors.full_messages.to_sentence.presence || e.message}"
27
+
28
+ referer = request.referer.to_s
29
+
30
+ if referer.end_with?(send(effective_resource.edit_path, resource))
31
+ @page_title ||= "Edit #{resource}"
32
+ render :edit
33
+ elsif referer.end_with?(send(effective_resource.new_path))
34
+ @page_title ||= "New #{resource_name.titleize}"
35
+ render :new
36
+ else
37
+ @page_title ||= resource.to_s
38
+ flash[:danger] = flash.now[:danger]
39
+
40
+ if referer.present? && (Rails.application.routes.recognize_path(URI(referer).path) rescue false)
41
+ redirect_back fallback_location: resource_redirect_path
42
+ else
43
+ redirect_to(resource_redirect_path)
44
+ end
45
+ end
46
+ end
47
+ end
17
48
  end
18
49
  end
50
+
19
51
  end
20
52
 
21
53
  def index
@@ -102,7 +134,7 @@ module Effective
102
134
  when 'Save' ; send(effective_resource.edit_path, resource)
103
135
  when 'Save and Continue' ; send(effective_resource.index_path)
104
136
  when 'Save and Add New' ; send(effective_resource.new_path)
105
- else send(effective_resource.show_path, resource)
137
+ else send((effective_resource.show_path(check: true) || effective_resource.edit_path), resource)
106
138
  end
107
139
  end
108
140
 
@@ -147,9 +179,14 @@ module Effective
147
179
  effective_resource.klass
148
180
  else
149
181
  case (resource_scope = send(resource_scope_method_name))
150
- when Hash ; effective_resource.klass.where(resource_scope)
151
- when Symbol ; effective_resource.klass.send(resource_scope)
152
- when nil ; effective_resource.klass
182
+ when ActiveRecord::Relation
183
+ effective_resource.relation.merge(resource_scope)
184
+ when Hash
185
+ effective_resource.klass.where(resource_scope)
186
+ when Symbol
187
+ effective_resource.klass.send(resource_scope)
188
+ when nil
189
+ effective_resource.klass
153
190
  else
154
191
  raise "expected #{resource_scope_method_name} to return a Hash or Symbol"
155
192
  end
@@ -161,6 +198,7 @@ module Effective
161
198
  return {} unless resource_scope_method_name.present?
162
199
 
163
200
  case (resource_scope = send(resource_scope_method_name))
201
+ when ActiveRecord::Relation ; {resource_scope: true}
164
202
  when Hash ; resource_scope
165
203
  when Symbol ; {resource_scope: true}
166
204
  when nil ; {}
@@ -11,16 +11,19 @@ module Effective
11
11
  end
12
12
 
13
13
  # All attributes from the klass, sorted as per attributes block.
14
- # Does not include :id, :created_at, :updated_at
15
- def klass_attributes
14
+ # Does not include :id, :created_at, :updated_at unless all is passed
15
+ def klass_attributes(all: false)
16
16
  attributes = (klass.new().attributes rescue nil)
17
17
  return [] unless attributes
18
18
 
19
- attributes = (attributes.keys - [klass.primary_key, 'created_at', 'updated_at'] - belong_tos.map { |reference| reference.foreign_key }).map do |att|
19
+ names = attributes.keys - belong_tos.map { |reference| reference.foreign_key }
20
+ names = names - [klass.primary_key, 'created_at', 'updated_at'] unless all
21
+
22
+ attributes = names.map do |name|
20
23
  if klass.respond_to?(:column_for_attribute) # Rails 4+
21
- Effective::Attribute.new(att, klass.column_for_attribute(att).type)
24
+ Effective::Attribute.new(name, klass.column_for_attribute(name).type)
22
25
  else
23
- Effective::Attribute.new(att, klass.columns_hash[att].type)
26
+ Effective::Attribute.new(name, klass.columns_hash[name].type)
24
27
  end
25
28
  end
26
29
 
@@ -12,8 +12,10 @@ module Effective
12
12
  _klass_by_name(input)
13
13
  when ActiveRecord::Relation
14
14
  input.klass
15
+ when (ActiveRecord::Reflection::AbstractReflection rescue :nil)
16
+ ((input.klass rescue nil).presence || _klass_by_name(input.class_name)) unless input.options[:polymorphic]
15
17
  when ActiveRecord::Reflection::MacroReflection
16
- input.klass unless input.options[:polymorphic]
18
+ ((input.klass rescue nil).presence || _klass_by_name(input.class_name)) unless input.options[:polymorphic]
17
19
  when ActionDispatch::Journey::Route
18
20
  _klass_by_name(input.defaults[:controller])
19
21
  when Class
@@ -42,6 +44,8 @@ module Effective
42
44
  return klass
43
45
  end
44
46
  end
47
+
48
+ nil
45
49
  end
46
50
 
47
51
  def _initialize_relation(input)
@@ -4,7 +4,7 @@ module Effective
4
4
  SPLIT = /\/|::/ # / or ::
5
5
 
6
6
  def name # 'post'
7
- @name ||= (klass.try(:name).to_s.split(SPLIT).last || '').singularize.downcase
7
+ @name ||= (klass.try(:name).to_s.split(SPLIT).last || '').singularize.underscore
8
8
  end
9
9
 
10
10
  def plural_name # 'posts'
@@ -186,11 +186,22 @@ module Effective
186
186
  .pluck(klass.primary_key)
187
187
  elsif association.macro == :has_many && association.options[:through].present?
188
188
  key = sql_column(klass.primary_key)
189
- values = relation.pluck(association.source_reflection.klass.primary_key).uniq.compact
190
189
 
191
- keys = association.through_reflection.klass
192
- .where(association.source_reflection.foreign_key => values)
193
- .pluck(association.through_reflection.foreign_key)
190
+ if association.source_reflection.options[:polymorphic]
191
+ reflected_klass = association.klass
192
+ else
193
+ reflected_klass = association.source_reflection.klass
194
+ end
195
+
196
+ values = relation.pluck(reflected_klass.primary_key).uniq.compact
197
+
198
+ scope = association.through_reflection.klass.where(association.source_reflection.foreign_key => values)
199
+
200
+ if association.source_reflection.options[:polymorphic]
201
+ scope = scope.where(association.source_reflection.foreign_type => reflected_klass.name)
202
+ end
203
+
204
+ keys = scope.pluck(association.through_reflection.foreign_key)
194
205
  elsif association.macro == :has_many
195
206
  key = sql_column(klass.primary_key)
196
207
  keys = relation.pluck(association.foreign_key)
@@ -20,6 +20,7 @@ module Effective
20
20
  end
21
21
 
22
22
  def max_id
23
+ return 999999 unless klass.respond_to?(:unscoped)
23
24
  @max_id ||= klass.unscoped.maximum(klass.primary_key).to_i
24
25
  end
25
26
 
@@ -1,3 +1,3 @@
1
1
  module EffectiveResources
2
- VERSION = '0.3.5'.freeze
2
+ VERSION = '0.3.6'.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: 0.3.5
4
+ version: 0.3.6
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: 2017-04-10 00:00:00.000000000 Z
11
+ date: 2017-04-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails