cookbook 0.1.4 → 0.1.5

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
  SHA256:
3
- metadata.gz: 7e2309d482dcbc455618dc9f39437be5227810e02cfb7140bb8e902e9b83b988
4
- data.tar.gz: 98ed806c1eda223158fe70c05cf606b9bd3c853fea44d5902f3c26d6725805e0
3
+ metadata.gz: ed17e18fbc21b1e5cd13d56061cbfb70cfa37390463f3b066c00216d990f791d
4
+ data.tar.gz: 6f027dbee389ea0ed9b33ded8a68aeaa9a941a1ee74b7ccb23600e555105ee5e
5
5
  SHA512:
6
- metadata.gz: 9f16ecde7b640d13cca89f65a08fb45299a99d2a1d3a99c68d66997031cbd378af94b66b2f20444f920b10424a07e21daf22f7f84028306ecfaa5821d682c688
7
- data.tar.gz: 5af1a7e1753ce68e95cc883aeb06cf62c8f3fb45b1826ff45014641eb207a4bbfa49b33eaaebfbb867cba1269410fc6d58d2c768f819a8d0d401ae3701e18f67
6
+ metadata.gz: 4944c3707a8612da91fee783427a50500de9bc3a03f3ae13395f29fb2124175222fb2f22f0f0de28df92bdf15444ed02e359e91c5f5d26ef2a5371c93d36b60a
7
+ data.tar.gz: ed9430324055b96146b3480f570403e42117e77d5a7594805e01f1391076d5e3739e163f0d0a6868f50d7a7bc5546c09ca905df5b4b744fe9470d10bff7e7fce
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.4
1
+ 0.1.5
data/cookbook.gemspec CHANGED
@@ -2,17 +2,17 @@
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
3
  # Instead, edit Juwelier::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
- # stub: cookbook 0.1.4 ruby lib
5
+ # stub: cookbook 0.1.5 ruby lib
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "cookbook".freeze
9
- s.version = "0.1.4"
9
+ s.version = "0.1.5"
10
10
 
11
11
  s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
12
12
  s.metadata = { "source_code_uri" => "http://github.com/gemvein/cookbook" } if s.respond_to? :metadata=
13
13
  s.require_paths = ["lib".freeze]
14
14
  s.authors = ["Loren Lundgren".freeze]
15
- s.date = "2021-09-16"
15
+ s.date = "2021-09-21"
16
16
  s.description = "Cookbook allows you to associate instructions with components in a cross referenced way. Good for cooking recipes or an instruction manual for DIY projects.".freeze
17
17
  s.email = "loren.lundgren@gmail.com".freeze
18
18
  s.executables = ["rails".freeze]
@@ -15,7 +15,24 @@ module Cookbook
15
15
 
16
16
  # Relationships
17
17
  has_many :uses, as: :use_in, class_name: 'Cookbook::Use', inverse_of: :use_in
18
+ # Used only as a setter, not a getter
19
+ accepts_nested_attributes_for :uses, reject_if: :all_blank, allow_destroy: true
18
20
  associate_used_in
21
+
22
+ used_in.each do |table_sym|
23
+ singular = table_sym.to_s.singularize
24
+ model = singular.classify.constantize
25
+ equals_method_symbol = "#{singular}_uses_attributes=".to_sym
26
+ define_method(equals_method_symbol) do |values|
27
+ items = []
28
+ values.each_pair do |key, value|
29
+ value['use_in'] = self
30
+ value['use_of'] = model.find_by(id: value['use_of_id'])
31
+ items << value
32
+ end
33
+ self.uses_attributes = items
34
+ end
35
+ end
19
36
  end
20
37
 
21
38
  # Extended by has_cookbook mixin
@@ -33,6 +50,8 @@ module Cookbook
33
50
  has_many uses_symbol, lambda {
34
51
  where(use_of_type: name)
35
52
  }, as: :use_in, class_name: 'Cookbook::Use', inverse_of: singular_symbol
53
+
54
+ # Used only as a getter, not a setter
36
55
  accepts_nested_attributes_for uses_symbol, reject_if: :all_blank, allow_destroy: true
37
56
 
38
57
  has_many table_sym, through: uses_symbol, source: :use_of, source_type: name
@@ -10,20 +10,20 @@ module Cookbook
10
10
  extend ClassMethods
11
11
  include InstanceMethods
12
12
 
13
- tables = model_symbols
13
+ self.use_of = model_symbols
14
14
  self.label_method = :name
15
15
  Cookbook::Use.add_use_of(table_name.to_sym)
16
16
 
17
17
  # Relationships
18
- has_many :uses, as: :use_of, class_name: 'Cookbook::Use'
19
- associate_uses_of(tables)
18
+ has_many :uses, as: :use_of, class_name: 'Cookbook::Use', inverse_of: :use_of
19
+ associate_uses_of
20
20
 
21
21
  if defined?(RailsAdmin)
22
22
  rails_admin do
23
23
  field :uses do
24
24
  visible false
25
25
  end
26
- tables.each do |table_sym| # We don't want these associations to show
26
+ self.use_of.each do |table_sym| # We don't want these associations to show
27
27
  table_uses_sym = "#{table_sym.to_s.singularize}_uses".to_sym
28
28
  field table_uses_sym do
29
29
  visible false
@@ -38,10 +38,10 @@ module Cookbook
38
38
 
39
39
  # Extended by acts_as_used_in mixin
40
40
  module ClassMethods
41
- attr_accessor :label_method
41
+ attr_accessor :label_method, :use_of
42
42
 
43
- def associate_uses_of(tables)
44
- tables.each do |table_sym|
43
+ def associate_uses_of
44
+ self.use_of.each do |table_sym|
45
45
  model = table_sym.to_s.classify.constantize
46
46
  name = model.model_name.to_s
47
47
 
Binary file
Binary file