effective_resources 1.0.0 → 1.0.1

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: 3ef79c2ff4b11e7b6fc7c81007b82f1ecb0fa8a0
4
- data.tar.gz: 98158d2296aac5e0290d515a1bf82e3a4ace8138
3
+ metadata.gz: c677c888d40faa747d421ffbeecbf1452e5328db
4
+ data.tar.gz: 2bf38b93293b53d35a86f1e9c551b1cfec07489f
5
5
  SHA512:
6
- metadata.gz: 1d49fb28d6eafa486291070b6e3aca1c2f8381f02598d5ef9d0a33f1bf449e701a8f8ecf6e76ae82bf463fc1b002f4e84e8d540eef1f64db2365ecbecffe91ed
7
- data.tar.gz: fdb49fb1b934cbf11428eaab0ee6906e813fa782f0b6dee6bfb936705b7edfa24a05d7f211b6370f67d0585a7e08b984c7d3e97c3f5538d6f297566ede2dc845
6
+ metadata.gz: ab530180ab92b4e5a866b34d38941d0234e73564c4291b02b661f675eb399d4bbd5fcf4445b6db935734da154a36f1a120255fe05839fcc7b814d92784a61cf2
7
+ data.tar.gz: dd14ce62821ee55c08ded91a33fca6a57a17ecf3a857d9aad138b7586b8590a0304252f652dc7ebb519799836dc3c100699e9b640bf42b014e74d7a45c11aa9e
@@ -1,4 +1,4 @@
1
- Copyright 2017 Code and Effect Inc.
1
+ Copyright 2018 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
@@ -23,10 +23,20 @@ module Effective
23
23
 
24
24
  self.resource ||= resource_scope.new
25
25
 
26
- self.resource.assign_attributes(
27
- params.to_unsafe_h.except(:controller, :action, :id).select { |k, v| resource.respond_to?("#{k}=") }
28
- )
26
+ # Assign any passed params
27
+ to_assign = if params[:_datatable_id].present?
28
+ inline_datatable = EffectiveDatatables.find(params[:_datatable_id])
29
+ inline_datatable.view = view_context
30
+ inline_datatable.attributes
31
+ elsif params.present?
32
+ params.to_unsafe_h.except(:controller, :action, :id, :duplicate_id)
33
+ end
34
+
35
+ if to_assign.present?
36
+ resource.assign_attributes(to_assign.select { |k, v| resource.respond_to?("#{k}=") })
37
+ end
29
38
 
39
+ # Duplicate if possible
30
40
  if params[:duplicate_id]
31
41
  duplicate = resource_scope.find(params[:duplicate_id])
32
42
  EffectiveResources.authorize!(self, :show, duplicate)
@@ -48,7 +48,9 @@ module Effective
48
48
  (effective_resource.namespaces & Array(permitted).map(&:to_s)).present?
49
49
  end
50
50
  end
51
- end.keys
51
+ end
52
+
53
+ permitted_params = permitted_params.map { |k, v| v.first == :array ? { k => [] } : k }
52
54
 
53
55
  # Recursively add any accepts_nested_resources
54
56
  effective_resource.nested_resources.each do |nested|
@@ -1,6 +1,6 @@
1
1
  module Effective
2
2
  class ModelReader
3
- DATATYPES = [:binary, :boolean, :date, :datetime, :decimal, :float, :hstore, :inet, :integer, :string, :text]
3
+ DATATYPES = [:binary, :boolean, :date, :datetime, :decimal, :float, :hstore, :inet, :integer, :string, :text, :permitted_param]
4
4
 
5
5
  attr_reader :attributes
6
6
 
@@ -18,6 +18,9 @@ module Effective
18
18
  return
19
19
  end
20
20
 
21
+ # Not really an attribute, just a permitted param.
22
+ args.unshift(:permitted_param) if args.first.kind_of?(Hash) && args.first.key?(:permitted)
23
+
21
24
  unless DATATYPES.include?(args.first)
22
25
  raise "expected first argument to be a datatype. Try name :string"
23
26
  end
@@ -8,7 +8,7 @@ module Effective
8
8
 
9
9
  def belong_tos
10
10
  return [] unless klass.respond_to?(:reflect_on_all_associations)
11
- @belong_tos ||= klass.reflect_on_all_associations(:belongs_to)
11
+ klass.reflect_on_all_associations(:belongs_to)
12
12
  end
13
13
 
14
14
  # author_id, post_id
@@ -16,24 +16,36 @@ module Effective
16
16
  belong_tos.map { |ass| (ass.options[:foreign_key] || "#{ass.name}_id").to_sym }
17
17
  end
18
18
 
19
+ def has_anys
20
+ (has_ones + has_manys + has_and_belongs_to_manys)
21
+ end
22
+
23
+ def has_manys_ids
24
+ (has_manys + has_and_belongs_to_manys).map { |ass| "#{ass.plural_name.singularize}_ids".to_sym }
25
+ end
26
+
19
27
  def has_ones
20
28
  return [] unless klass.respond_to?(:reflect_on_all_associations)
21
- @has_ones ||= klass.reflect_on_all_associations(:has_one)
29
+ klass.reflect_on_all_associations(:has_one)
30
+ end
31
+
32
+ def has_ones_ids
33
+ has_ones.map { |ass| "#{ass.name}_id".to_sym }
22
34
  end
23
35
 
24
36
  def has_manys
25
37
  return [] unless klass.respond_to?(:reflect_on_all_associations)
26
- @has_manys ||= klass.reflect_on_all_associations(:has_many).reject { |ass| ass.options[:autosave] }
38
+ klass.reflect_on_all_associations(:has_many).reject { |ass| ass.options[:autosave] }
27
39
  end
28
40
 
29
41
  def has_and_belongs_to_manys
30
42
  return [] unless klass.respond_to?(:reflect_on_all_associations)
31
- @has_and_belongs_to_manys ||= klass.reflect_on_all_associations(:has_and_belongs_to_many)
43
+ klass.reflect_on_all_associations(:has_and_belongs_to_many)
32
44
  end
33
45
 
34
46
  def nested_resources
35
47
  return [] unless klass.respond_to?(:reflect_on_all_associations)
36
- @nested_resources ||= klass.reflect_on_all_associations(:has_many).select { |ass| ass.options[:autosave] }
48
+ klass.reflect_on_all_associations(:has_many).select { |ass| ass.options[:autosave] }
37
49
  end
38
50
 
39
51
  def associated(name)
@@ -8,31 +8,64 @@ module Effective
8
8
  (klass_attributes.presence || model_attributes.presence)
9
9
  end
10
10
 
11
+ def primary_key_attribute
12
+ {klass.primary_key.to_sym => [:integer]}
13
+ end
14
+
11
15
  # The attributes for each belongs_to
12
16
  # { :client_id => [:integer], ... }
13
17
  def belong_tos_attributes
14
18
  belong_tos.inject({}) do |h, ass|
15
19
  unless ass.foreign_key == 'site_id' && ass.respond_to?(:acts_as_site_specific)
16
- h[ass.foreign_key.to_sym] = [:integer]
20
+ h[ass.foreign_key.to_sym] = [:integer, :index => true]
17
21
  end; h
18
22
  end
19
23
  end
20
24
 
25
+ def has_manys_attributes
26
+ has_manys_ids.inject({}) { |h, ass| h[ass] = [:array]; h }
27
+ end
28
+
29
+ # All will include primary_key, created_at, updated_at and belongs_tos
21
30
  # This is the attributes as defined by the effective_resources do .. end block
22
31
  # { :name => [:string, { permitted: false }], ... }
23
- def model_attributes
24
- model ? model.attributes : {}
32
+ def model_attributes(all: false)
33
+ atts = (model ? model.attributes : {})
34
+
35
+ if all # Probably being called by permitted_attributes
36
+ primary_key_attribute.merge(belong_tos_attributes).merge(has_manys_attributes).merge(atts)
37
+ else # This is the migrator. This should match table_attributes
38
+ belong_tos_attributes.merge(atts.reject { |_, v| v[0] == :permitted_param })
39
+ end
40
+ end
41
+
42
+ # All table attributes. includes primary_key and belongs_tos
43
+ def table_attributes
44
+ attributes = (klass.new().attributes rescue nil)
45
+ return {} unless attributes
46
+
47
+ (attributes.keys - [klass.primary_key]).inject({}) do |h, name|
48
+ if klass.respond_to?(:column_for_attribute) # Rails 4+
49
+ h[name.to_sym] = [klass.column_for_attribute(name).type]
50
+ else
51
+ h[name.to_sym] = [klass.columns_hash[name].type]
52
+ end; h
53
+ end
25
54
  end
26
55
 
27
56
  # Used by effective_crud_controller to generate the permitted params
28
57
  def permitted_attributes
29
- id = {klass.primary_key.to_sym => [:integer]}
30
- bts = belong_tos_ids.inject({}) { |h, ass| h[ass] = [:integer]; h }
31
-
32
- id.merge(bts).merge(model_attributes)
58
+ # id = {klass.primary_key.to_sym => [:integer]}
59
+ # bts = belong_tos_ids.inject({}) { |h, ass| h[ass] = [:integer]; h }
60
+ # has_manys = has_manys_ids.inject({}) { |h, ass| h[ass] = [:array]; h }
61
+ # has_manys.each { |k, _| has_manys[k] = model_attributes[k] if model_attributes.key?(k) }
62
+ # Does not include nested, as they are added recursively elsewhere
63
+ # id.merge(bts).merge(model_attributes).merge(has_manys)
64
+
65
+ model_attributes(all: true)
33
66
  end
34
67
 
35
- # All attributes from the klass, sorted as per attributes block.
68
+ # All attributes from the klass, sorted as per model attributes block.
36
69
  # Does not include :id, :created_at, :updated_at unless all is passed
37
70
  def klass_attributes(all: false)
38
71
  attributes = (klass.new().attributes rescue nil)
@@ -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 }
16
+ submits['Continue'] = { action: :save, redirect: :index, default: true, unless: -> { 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 }
20
+ submits['Add New'] = { action: :save, redirect: :new, default: true, unless: -> { params[:_datatable_id] } }
21
21
  end
22
22
  end
23
23
  end
@@ -7,7 +7,7 @@ module Effective
7
7
  @model = ModelReader.new(&block)
8
8
 
9
9
  # If effective_developer is in live mode, this will cause it to refresh the class
10
- ActiveSupport.run_load_hooks(:effective_resource, klass)
10
+ ActiveSupport.run_load_hooks(:effective_resource, self)
11
11
  end
12
12
 
13
13
  def model
@@ -1,3 +1,3 @@
1
1
  module EffectiveResources
2
- VERSION = '1.0.0'.freeze
2
+ VERSION = '1.0.1'.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.0
4
+ version: 1.0.1
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-09-12 00:00:00.000000000 Z
11
+ date: 2018-09-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails