acts_as_has_many 0.1.2 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rspec +4 -0
- data/CHANGELOG.md +23 -1
- data/Gemfile +1 -3
- data/README.md +44 -4
- data/Rakefile +7 -2
- data/lib/acts_as_has_many/version.rb +1 -1
- data/lib/acts_as_has_many.rb +6 -6
- metadata +103 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: afc94ec7746ca6ad877adc8ea9942643c6d7c04fc0653fd0ecfe4453955202ca
|
4
|
+
data.tar.gz: b67e54b03a0fedef679306bc6e0a56609ce119da645f8e3de709f57cd29be6bf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e1542d91fdce83668a7ffd0ea3239bd153829adf409ac9381d6ac712cf52f8b3fbfdc551616a1cca39b66a96de64be9918883199d54491b0fde349c19f2e26b0
|
7
|
+
data.tar.gz: 61e7c75d48216eff75b252492df8386dee3f2ee1539cbf47db0f6d61253510e59a0a12f786ca112bdb623e9947df9675e588db747e746540147316585d78d28a
|
data/.rspec
ADDED
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,27 @@
|
|
1
1
|
## [Unreleased]
|
2
2
|
|
3
|
-
## [0.1.
|
3
|
+
## [0.1.1]
|
4
4
|
|
5
5
|
- Initial release
|
6
|
+
|
7
|
+
## [0.1.2]
|
8
|
+
|
9
|
+
- separate method for acts_as_accepts_nested_attributes_for
|
10
|
+
|
11
|
+
## [0.1.3]
|
12
|
+
|
13
|
+
- Added unit tests
|
14
|
+
|
15
|
+
- fix(): convert attribute to string before classifing
|
16
|
+
|
17
|
+
- Add changelogs
|
18
|
+
|
19
|
+
- fix lint issues with rubocop
|
20
|
+
|
21
|
+
- use map + compact instead of filter_map to support older versions of ruby
|
22
|
+
|
23
|
+
## [0.1.4]
|
24
|
+
|
25
|
+
- Updated Readme
|
26
|
+
|
27
|
+
- coverall setup
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,8 +1,6 @@
|
|
1
1
|
# ActsAsHasMany
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
TODO: Delete this and the text above, and describe your gem
|
3
|
+
ActsAsHasMany mimics a has_many association within an array stored in your ActiveRecord models. It provides a flexible way to define and manage custom associations using array attributes, without relying solely on standard ActiveRecord associations. Additionally, it supports nested forms by mimicking accepts_nested_attributes_for.
|
6
4
|
|
7
5
|
## Installation
|
8
6
|
|
@@ -16,7 +14,49 @@ If bundler is not being used to manage dependencies, install the gem by executin
|
|
16
14
|
|
17
15
|
## Usage
|
18
16
|
|
19
|
-
|
17
|
+
Use the acts_as_has_many method to define custom array associations within your model:
|
18
|
+
|
19
|
+
```
|
20
|
+
class MyModel < ActiveRecord::Base
|
21
|
+
acts_as_has_many :some_attribute
|
22
|
+
end
|
23
|
+
```
|
24
|
+
|
25
|
+
`some_attribute` is an attrubute with in your model, and is expected to return an Array. It could be of any type `jsonb`, `text`, etc.
|
26
|
+
|
27
|
+
The gem will look for a class with the same name as the attribute, eg. `RelatedObject`
|
28
|
+
|
29
|
+
### Custom Class and OpenStruct
|
30
|
+
|
31
|
+
You can also specify custom class names for your associations.
|
32
|
+
|
33
|
+
```
|
34
|
+
class MyModel < ActiveRecord::Base
|
35
|
+
acts_as_has_many :some_attribute, class_name: 'OtherClass'
|
36
|
+
end
|
37
|
+
```
|
38
|
+
|
39
|
+
If you prefer not to create a new class, you can use OpenStruct.
|
40
|
+
|
41
|
+
```
|
42
|
+
class MyModel < ActiveRecord::Base
|
43
|
+
acts_as_has_many :some_attribute, class_name: 'OpenStruct'
|
44
|
+
end
|
45
|
+
```
|
46
|
+
|
47
|
+
### Custom Attribute
|
48
|
+
|
49
|
+
By default, ActsAsHasMany overrides the attribute getter and setter methods. If you prefer to use different attribute, you can specify a custom attribute using the :attribute option:
|
50
|
+
|
51
|
+
```
|
52
|
+
class MyModel < ActiveRecord::Base
|
53
|
+
acts_as_has_many :some_attribute, attribute: :some_other_attribute
|
54
|
+
end
|
55
|
+
```
|
56
|
+
|
57
|
+
## Support and Contact
|
58
|
+
|
59
|
+
If you have any questions, issues, or feedback, please open an issue on the GitHub repository.
|
20
60
|
|
21
61
|
## Development
|
22
62
|
|
data/Rakefile
CHANGED
data/lib/acts_as_has_many.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require_relative
|
3
|
+
require_relative 'acts_as_has_many/version'
|
4
4
|
|
5
5
|
module ActsAsHasMany
|
6
6
|
def self.included(base)
|
@@ -12,8 +12,8 @@ module ActsAsHasMany
|
|
12
12
|
validates attribute, associated: true
|
13
13
|
|
14
14
|
column = options[:attribute].presence || attribute
|
15
|
-
klass = options[:class_name].presence || attribute.classify
|
16
|
-
|
15
|
+
klass = options[:class_name].presence || attribute.to_s.classify
|
16
|
+
|
17
17
|
define_method(attribute.to_sym) do
|
18
18
|
@options ||= read_attribute(column).map do |option|
|
19
19
|
klass.constantize.new(option)
|
@@ -25,11 +25,11 @@ module ActsAsHasMany
|
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
28
|
-
def acts_as_accepts_nested_attributes_for(attribute,
|
28
|
+
def acts_as_accepts_nested_attributes_for(attribute, _options = {})
|
29
29
|
define_method("#{attribute}_attributes=".to_sym) do |associates|
|
30
|
-
writeable_associates = associates.values.
|
30
|
+
writeable_associates = associates.values.map do |associate|
|
31
31
|
associate.except('_destroy') unless associate['_destroy'].present?
|
32
|
-
end
|
32
|
+
end.compact
|
33
33
|
|
34
34
|
public_send("#{attribute}=".to_sym, writeable_associates)
|
35
35
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: acts_as_has_many
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Pralish Kayastha
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-10-
|
11
|
+
date: 2023-10-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -16,14 +16,112 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: '6.0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
26
|
+
version: '6.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: sqlite3
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: appraisal
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '2.1'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '2.1'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: coveralls_reborn
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: simplecov-lcov
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
27
125
|
description: acts_as_has_many is a gem that makes it easy to work with JSONB arrays
|
28
126
|
as Active Record associations in your Ruby on Rails applications. It simplifies
|
29
127
|
nested form handling.
|
@@ -33,6 +131,7 @@ executables: []
|
|
33
131
|
extensions: []
|
34
132
|
extra_rdoc_files: []
|
35
133
|
files:
|
134
|
+
- ".rspec"
|
36
135
|
- CHANGELOG.md
|
37
136
|
- CODE_OF_CONDUCT.md
|
38
137
|
- Gemfile
|