cookbook 0.1.1 → 0.1.5

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
  SHA256:
3
- metadata.gz: 66fccc09592729e8aab5d9403f01231956d438110e706976b10c5bbfd30d7984
4
- data.tar.gz: 5914fed8d04d5db8fd409e22e627f8f0461a4908a1c6fa21fd328c5d042da7ee
3
+ metadata.gz: ed17e18fbc21b1e5cd13d56061cbfb70cfa37390463f3b066c00216d990f791d
4
+ data.tar.gz: 6f027dbee389ea0ed9b33ded8a68aeaa9a941a1ee74b7ccb23600e555105ee5e
5
5
  SHA512:
6
- metadata.gz: 8db914804526e40973d712443bdf7d5d096b8e8a838bf0a80590dfb5cf95d937b5e2d95b419fcf24d4c1841464b776aa4723ad35c30a51b7e9f97fa5ddc0e659
7
- data.tar.gz: 4e848e7186bb761911e2bda14ef95587c0b6d1323cbc0a2a2be0edc25411a9bf4c4030cc90ca005a4771b3b1fcf86b61ed32b02ee8c2b90624bd34ce8806c127
6
+ metadata.gz: 4944c3707a8612da91fee783427a50500de9bc3a03f3ae13395f29fb2124175222fb2f22f0f0de28df92bdf15444ed02e359e91c5f5d26ef2a5371c93d36b60a
7
+ data.tar.gz: ed9430324055b96146b3480f570403e42117e77d5a7594805e01f1391076d5e3739e163f0d0a6868f50d7a7bc5546c09ca905df5b4b744fe9470d10bff7e7fce
data/Gemfile CHANGED
@@ -5,7 +5,7 @@ source 'https://rubygems.org'
5
5
  gem 'rails', '>= 6', '< 7'
6
6
 
7
7
  gem 'haml-rails', '>= 2.0', '< 3'
8
- gem 'sass-rails', '>= 6', '< 7'
8
+ gem 'sass-rails', '>= 5', '< 7'
9
9
 
10
10
  gem 'cancancan', '>= 3.3', '< 4'
11
11
 
data/Gemfile.lock CHANGED
@@ -155,18 +155,6 @@ GEM
155
155
  rdoc
156
156
  semver
157
157
  jwt (2.2.3)
158
- kaminari (1.2.1)
159
- activesupport (>= 4.1.0)
160
- kaminari-actionview (= 1.2.1)
161
- kaminari-activerecord (= 1.2.1)
162
- kaminari-core (= 1.2.1)
163
- kaminari-actionview (1.2.1)
164
- actionview
165
- kaminari-core (= 1.2.1)
166
- kaminari-activerecord (1.2.1)
167
- activerecord
168
- kaminari-core (= 1.2.1)
169
- kaminari-core (1.2.1)
170
158
  loofah (2.12.0)
171
159
  crass (~> 1.0.2)
172
160
  nokogiri (>= 1.5.9)
@@ -347,7 +335,6 @@ DEPENDENCIES
347
335
  faker
348
336
  haml-rails (>= 2.0, < 3)
349
337
  juwelier (~> 2.1.0)
350
- kaminari (>= 1.2.1, < 2)
351
338
  puma (>= 4.3, < 5)
352
339
  rails (>= 6, < 7)
353
340
  rspec-html-matchers
@@ -355,7 +342,7 @@ DEPENDENCIES
355
342
  rubocop
356
343
  rubocop-rails
357
344
  rubocop-rspec
358
- sass-rails (>= 6, < 7)
345
+ sass-rails (>= 5, < 7)
359
346
  shoulda
360
347
  shoulda-matchers (>= 4.3, < 5)
361
348
  simple_form (>= 5.1, < 6)
data/README.md CHANGED
@@ -3,8 +3,6 @@ Works Cited allows you to add a list of the works cited in ActiveRecord objects,
3
3
 
4
4
  Works Cited can be configured to use CanCanCan to authorize the editing of citations. This makes it easy for you to control access.
5
5
 
6
- Works Cited does not require, but is compatible with Rails Admin.
7
-
8
6
  ## Installation
9
7
  Add this line to your application's Gemfile:
10
8
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.1.5
@@ -1,17 +1,37 @@
1
1
  # frozen_string_literal: true
2
+ require 'pp'
2
3
 
3
4
  module Cookbook
4
5
  # Cookbook::Use is basically a linking table with extra information.
5
6
  class Use < ApplicationRecord
7
+ attr_accessor(:used_in_tables)
8
+
6
9
  # Relationships
7
- belongs_to :use_in, polymorphic: true
8
- belongs_to :use_of, polymorphic: true
10
+ belongs_to :use_in, polymorphic: true, inverse_of: :uses
11
+ belongs_to :use_of, polymorphic: true, inverse_of: :uses
12
+
13
+ class << self
14
+ def add_use_of(table_sym)
15
+ singular = table_sym.to_s.singularize
16
+ model_name = singular.classify.constantize.to_s
17
+ belongs_to singular.to_sym, class_name: model_name, foreign_key: :use_of_id, inverse_of: :uses, optional: true
18
+ end
9
19
 
10
- # Scopes
11
- scope :recipe_uses, -> { where use_in_type: 'Recipe' }
12
- scope :ingredient_uses, -> { where use_of_type: 'Ingredient' }
20
+ def add_use_in(table_sym)
21
+ singular = table_sym.to_s.singularize
22
+ model_name = singular.classify.constantize.to_s
23
+ belongs_to singular.to_sym, class_name: model_name, foreign_key: :use_in_id, inverse_of: :uses, optional: true
24
+ end
25
+ end
13
26
 
14
27
  # Methods
28
+ def object_label
29
+ return use_of.name if use_of.respond_to?(:name)
30
+ return use_of.title if use_of.respond_to?(:title)
31
+
32
+ super
33
+ end
34
+
15
35
  def quantity_with_unit
16
36
  [quantity, unit].compact.join(' ')
17
37
  end
@@ -26,11 +46,9 @@ module Cookbook
26
46
  if defined?(RailsAdmin)
27
47
  rails_admin do
28
48
  visible false
49
+ object_label_method { :object_label }
29
50
  edit do
30
51
  include_all_fields
31
- field :use_in do
32
- visible false
33
- end
34
52
  end
35
53
  end
36
54
  end
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.1 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.1"
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-13"
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]
@@ -192,7 +192,7 @@ Gem::Specification.new do |s|
192
192
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
193
193
  s.add_runtime_dependency(%q<rails>.freeze, [">= 6", "< 7"])
194
194
  s.add_runtime_dependency(%q<haml-rails>.freeze, [">= 2.0", "< 3"])
195
- s.add_runtime_dependency(%q<sass-rails>.freeze, [">= 6", "< 7"])
195
+ s.add_runtime_dependency(%q<sass-rails>.freeze, [">= 5", "< 7"])
196
196
  s.add_runtime_dependency(%q<cancancan>.freeze, [">= 3.3", "< 4"])
197
197
  s.add_runtime_dependency(%q<simple_form>.freeze, [">= 5.1", "< 6"])
198
198
  s.add_runtime_dependency(%q<vanilla_nested>.freeze, [">= 1.3", "< 2"])
@@ -210,7 +210,7 @@ Gem::Specification.new do |s|
210
210
  else
211
211
  s.add_dependency(%q<rails>.freeze, [">= 6", "< 7"])
212
212
  s.add_dependency(%q<haml-rails>.freeze, [">= 2.0", "< 3"])
213
- s.add_dependency(%q<sass-rails>.freeze, [">= 6", "< 7"])
213
+ s.add_dependency(%q<sass-rails>.freeze, [">= 5", "< 7"])
214
214
  s.add_dependency(%q<cancancan>.freeze, [">= 3.3", "< 4"])
215
215
  s.add_dependency(%q<simple_form>.freeze, [">= 5.1", "< 6"])
216
216
  s.add_dependency(%q<vanilla_nested>.freeze, [">= 1.3", "< 2"])
@@ -229,7 +229,7 @@ Gem::Specification.new do |s|
229
229
  else
230
230
  s.add_dependency(%q<rails>.freeze, [">= 6", "< 7"])
231
231
  s.add_dependency(%q<haml-rails>.freeze, [">= 2.0", "< 3"])
232
- s.add_dependency(%q<sass-rails>.freeze, [">= 6", "< 7"])
232
+ s.add_dependency(%q<sass-rails>.freeze, [">= 5", "< 7"])
233
233
  s.add_dependency(%q<cancancan>.freeze, [">= 3.3", "< 4"])
234
234
  s.add_dependency(%q<simple_form>.freeze, [">= 5.1", "< 6"])
235
235
  s.add_dependency(%q<vanilla_nested>.freeze, [">= 1.3", "< 2"])
@@ -11,10 +11,28 @@ module Cookbook
11
11
  include InstanceMethods
12
12
 
13
13
  self.used_in = model_symbols
14
+ Cookbook::Use.add_use_in(table_name.to_sym)
14
15
 
15
16
  # Relationships
16
- has_many :uses, as: :use_in, class_name: 'Cookbook::Use'
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
17
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
18
36
  end
19
37
 
20
38
  # Extended by has_cookbook mixin
@@ -27,20 +45,31 @@ module Cookbook
27
45
  model = table_sym.to_s.classify.constantize
28
46
  name = model.model_name.to_s
29
47
  uses_symbol = "#{model.model_name.param_key}_uses".to_sym
48
+ singular_symbol = table_sym.to_s.singularize.to_sym
30
49
 
31
50
  has_many uses_symbol, lambda {
32
51
  where(use_of_type: name)
33
- }, as: :use_in, class_name: 'Cookbook::Use'
52
+ }, as: :use_in, class_name: 'Cookbook::Use', inverse_of: singular_symbol
53
+
54
+ # Used only as a getter, not a setter
34
55
  accepts_nested_attributes_for uses_symbol, reject_if: :all_blank, allow_destroy: true
35
56
 
36
- has_many table_sym, through: :uses, source: :use_of, source_type: name
57
+ has_many table_sym, through: uses_symbol, source: :use_of, source_type: name
37
58
 
38
59
  if defined?(RailsAdmin)
39
60
  rails_admin do
61
+ include_all_fields
40
62
  field :uses do
41
63
  visible false
42
64
  end
43
65
  tables.each do |table_sym|
66
+ singular = table_sym.to_s.singularize
67
+ uses_symbol = "#{singular}_uses".to_sym
68
+ field uses_symbol do # Show only on reload
69
+ visible do
70
+ !bindings[:object].new_record?
71
+ end
72
+ end
44
73
  field table_sym do # We don't want these associations to show
45
74
  visible false
46
75
  end
@@ -10,20 +10,38 @@ module Cookbook
10
10
  extend ClassMethods
11
11
  include InstanceMethods
12
12
 
13
- self.uses_of = model_symbols
13
+ self.use_of = model_symbols
14
14
  self.label_method = :name
15
+ Cookbook::Use.add_use_of(table_name.to_sym)
15
16
 
16
17
  # Relationships
17
- has_many :uses, as: :use_of, class_name: 'Cookbook::Use'
18
+ has_many :uses, as: :use_of, class_name: 'Cookbook::Use', inverse_of: :use_of
18
19
  associate_uses_of
20
+
21
+ if defined?(RailsAdmin)
22
+ rails_admin do
23
+ field :uses do
24
+ visible false
25
+ end
26
+ self.use_of.each do |table_sym| # We don't want these associations to show
27
+ table_uses_sym = "#{table_sym.to_s.singularize}_uses".to_sym
28
+ field table_uses_sym do
29
+ visible false
30
+ end
31
+ field table_sym do
32
+ visible false
33
+ end
34
+ end
35
+ end
36
+ end
19
37
  end
20
38
 
21
39
  # Extended by acts_as_used_in mixin
22
40
  module ClassMethods
23
- attr_accessor :uses_of, :label_method
41
+ attr_accessor :label_method, :use_of
24
42
 
25
43
  def associate_uses_of
26
- uses_of.each do |table_sym|
44
+ self.use_of.each do |table_sym|
27
45
  model = table_sym.to_s.classify.constantize
28
46
  name = model.model_name.to_s
29
47
 
Binary file
Binary file