muffin 0.2.0 → 0.3.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
2
  SHA1:
3
- metadata.gz: f444194037bfb0ce4da565e15de9365aba611a72
4
- data.tar.gz: 58ab1e839e36c8070e4f4cb24bc4af5ddbc87b25
3
+ metadata.gz: 2fd061494a4e7fef968dd4351c7404bd089c209a
4
+ data.tar.gz: f86718d799b5f72adf9156b1fcbd2ee85477eff8
5
5
  SHA512:
6
- metadata.gz: f4899b5cf39c11bf4c64561bec040877e71aa43d6a443d8fbe0eae6a1de87aa51d3c3a47df6f549793ea9be76e4ad2bf2fb7ade4d61c235daaa470ef27abd047
7
- data.tar.gz: 820ec3cb4faf5e037fa565feceb28c83d3fc102de599cd2de0bf831363ef78fd6ca2b2bbeb20d5789932df5c7eee5e09cb28d59e26616766e0a717c9e6f72b26
6
+ metadata.gz: 87007332acf8b0753272f6bef8d6c6dab614ca2d010f405ce2b57bad2c8dda6c8ec7dfcac8bf21969bbfac193167569d8ae52e4c8185f1119138f362d7cbe184
7
+ data.tar.gz: 7487d1aa3ea54e1b98e0823657fae557e29e4ea7ddbc74f8f197ab8aa35f8bd50af7e43044af186807f035421ea5bc200bd79a383837d34bd12279986977e9f3
@@ -27,7 +27,14 @@ module Muffin
27
27
  def convert(value, access_array: false)
28
28
  return convert(default) if value == nil && default
29
29
  return (value || []).map { |e| convert(e, access_array: true) } if array? && !access_array
30
- if type <= Integer
30
+ return value.deep_dup if value.is_a? type
31
+ return value if value.nil?
32
+
33
+ if type <= DateTime
34
+ value.to_s.in_time_zone(Time.zone).to_datetime if value.present?
35
+ elsif type <= Date # needs to come *after* DateTime because DateTime <= Date
36
+ Date.parse(value) if value.present?
37
+ elsif type <= Integer
31
38
  value&.to_i
32
39
  elsif type <= Float
33
40
  value&.to_f
@@ -35,6 +42,8 @@ module Muffin
35
42
  value&.to_s
36
43
  elsif type <= Symbol
37
44
  value.class <= Integer ? value.to_s.to_sym : value&.to_sym
45
+ elsif type <= Time
46
+ Time.parse(value).in_time_zone if value.present?
38
47
  elsif type <= BigDecimal
39
48
  type.new(value) if value.present?
40
49
  elsif type <= Hash
@@ -27,6 +27,10 @@ module Muffin
27
27
  attributes[name]
28
28
  end
29
29
 
30
+ def reflect_on_association(name)
31
+ OpenStruct.new klass: attributes[name].type
32
+ end
33
+
30
34
  private
31
35
 
32
36
  def define_class(name, block)
@@ -74,7 +78,7 @@ module Muffin
74
78
 
75
79
  validates_with Muffin::Validation::NestedAttributesValidator
76
80
 
77
- def initialize(attributes)
81
+ def initialize(attributes = {})
78
82
  self.attributes = attributes
79
83
  end
80
84
  end
@@ -4,6 +4,7 @@ module Muffin
4
4
  return false unless valid?
5
5
  permit!
6
6
  perform if respond_to? :perform
7
+ true
7
8
  end
8
9
 
9
10
  def call!
@@ -40,9 +40,8 @@ module Muffin
40
40
  end
41
41
 
42
42
  def attribute_value_permitted?(name, value)
43
- block = self.class.attributes[name]&.permitted_values
44
- return true unless block
45
- instance_exec(&block).include? value
43
+ return true if permitted_values(name).nil?
44
+ Array.wrap(value).all? { |e| permitted_values(name).include? e }
46
45
  end
47
46
 
48
47
  def self.included(base)
@@ -2,7 +2,7 @@ module Muffin
2
2
  module Sync
3
3
  private
4
4
 
5
- def update_nested!(relation, entities = [self])
5
+ def update_nested!(relation, entities = [self], synced_attributes: nil)
6
6
  entities = Array.wrap(entities)
7
7
 
8
8
  # load all (available) records upfront to avoid fetching them one by one in the loop
@@ -26,14 +26,16 @@ module Muffin
26
26
  if entity.try(:id).presence
27
27
  record = records[entity.id]
28
28
 
29
- if entity.try(:_destroy).presence
29
+ if entity.try(:_destroy)
30
30
  record.destroy
31
31
  else
32
- record.assign_attributes(model_attributes)
33
- record.save! if record.changed?
32
+ record.assign_attributes(model_attributes.slice(*(synced_attributes || record.attributes.keys.map(&:to_sym))))
33
+ record.save!
34
34
  end
35
35
  else
36
- record = relation.create!(model_attributes)
36
+ record = relation.build
37
+ record.assign_attributes(model_attributes.slice(*(synced_attributes || record.attributes.keys.map(&:to_sym))))
38
+ record.save!
37
39
  end
38
40
 
39
41
  association_attributes.each do |k, v|
@@ -5,8 +5,8 @@ module Muffin
5
5
  module ControllerAdditions
6
6
  def prepare(operation, params: nil, request: nil, scope: nil)
7
7
  if params.blank? && respond_to?(:params) && operation.respond_to?(:model_name)
8
- if self.params.key?(operation.model_name.underscore)
9
- params = self.params[operation.model_name.underscore].deep_dup.permit!.to_h
8
+ if self.params.key?(operation.model_name.param_key)
9
+ params = parse_arrays self.params[operation.model_name.param_key].deep_dup.permit!.to_h
10
10
  params.deep_transform_keys! do |key|
11
11
  if key.to_s[/.+_attributes\Z/]
12
12
  new_key = key.to_s.sub(/_attributes\Z/, "")
@@ -24,6 +24,21 @@ module Muffin
24
24
 
25
25
  operation.new(params: params, request: request, scope: scope)
26
26
  end
27
+
28
+ private
29
+
30
+ def parse_arrays(obj)
31
+ obj.each do |key, value|
32
+ if value.is_a? Hash
33
+ if value.keys.find { |k, _| k =~ /\D/ }
34
+ parse_arrays(value)
35
+ else
36
+ obj[key] = value.values
37
+ value.each_value { |h| parse_arrays(h) }
38
+ end
39
+ end
40
+ end
41
+ end
27
42
  end
28
43
  end
29
44
  end
@@ -1,3 +1,3 @@
1
1
  module Muffin
2
- VERSION = "0.2.0".freeze
2
+ VERSION = "0.3.0".freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: muffin
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jens Ravens
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2017-07-26 00:00:00.000000000 Z
12
+ date: 2018-05-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activemodel