Skip to content
GitLab
    • Explore Projects Groups Snippets
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • T torque-postgresql
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 6
    • Issues 6
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 1
    • Merge requests 1
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • Carlos
  • torque-postgresql
  • Merge requests
  • !57

BelongsToMany fixes

  • Review changes

  • Download
  • Email patches
  • Plain diff
Closed Administrator requested to merge github/fork/yosiat/belongs-to-many-fix-include-on-empty-list into master 4 years ago
  • Overview 1
  • Commits 4
  • Pipelines 0
  • Changes 3

Created by: yosiat

  • Fix include? when the list is null - https://github.com/crashtech/torque-postgresql/pull/57/commits/3fe2964e215e119d255e4e12c9e8792a5fd52172
  • Fix record delete - https://github.com/crashtech/torque-postgresql/pull/57/commits/ba278101f93541b08f145b8b1f5873014ffbaa51
  • Fix saving has_many, while belongs_to_many association is fetched on after_create - https://github.com/crashtech/torque-postgresql/pull/57/commits/f1458fd8b4c0e17d041d8f79e8f04ae94f1f75f4
Compare
  • master (base)

and
  • latest version
    f1458fd8
    4 commits, 2 years ago

3 files
+ 32
- 2

    Preferences

    File browser
    Compare changes
lib/torque/postgr‎esql/associations‎
belongs_to_many‎_association.rb‎ +9 -2
spec/‎tests‎
belongs_to_‎many_spec.rb‎ +12 -0
has_many‎_spec.rb‎ +11 -0
lib/torque/postgresql/associations/belongs_to_many_association.rb
+ 9
- 2
  • View file @ f1458fd8


@@ -11,6 +11,10 @@ module Torque
## CUSTOM
def ids_reader
if stale_target?
return stale_state
end
if loaded?
target.pluck(reflection.association_primary_key)
elsif !target.empty?
@@ -47,7 +51,7 @@ module Torque
return include_in_memory?(record) if record.new_record?
(!target.empty? && target.include?(record)) ||
stale_state.include?(record.read_attribute(klass_attr))
stale_state&.include?(record.read_attribute(klass_attr))
end
def load_target
@@ -154,6 +158,8 @@ module Torque
def delete_records(records, method)
ids = read_records_ids(records)
@target -= records
if method == :destroy
records.each(&:destroy!)
ids_rewriter(ids, :-)
@@ -179,7 +185,8 @@ module Torque
def ids_rewriter(ids, operator)
list = owner[source_attr] ||= []
list = list.public_send(operator, ids)
owner[source_attr] = list.uniq.compact
list = list.uniq.compact.presence
owner[source_attr] = list
return if @_building_changes || !owner.persisted?
owner.update_attribute(source_attr, list)
spec/tests/belongs_to_many_spec.rb
+ 12
- 0
  • View file @ f1458fd8


@@ -208,6 +208,15 @@ RSpec.describe 'BelongsToMany' do
expect(subject.tags.size).to be_eql(0)
end
it 'can delete specific records' do
tag = FactoryBot.create(:tag)
subject.tags << tag
expect(subject.tags.size).to be_eql(1)
subject.tags.delete(tag)
expect(subject.reload.tags.size).to be_eql(0)
end
it 'can destroy all records' do
subject.tags.concat(FactoryBot.create_list(:tag, 5))
expect(subject.tags.size).to be_eql(5)
@@ -244,6 +253,9 @@ RSpec.describe 'BelongsToMany' do
it 'can check if a record is included on the list' do
outside = FactoryBot.create(:tag)
expect(subject.tags.include?(outside)).to be_falsey
inside = FactoryBot.create(:tag)
subject.tags << inside
spec/tests/has_many_spec.rb
+ 11
- 0
  • View file @ f1458fd8


@@ -291,6 +291,17 @@ RSpec.describe 'HasMany' do
expect(record.tag_ids).to be_eql([subject.id])
end
it 'perists after they are accessed in after_create' do
other.belongs_to_many(:tags)
other.after_create { self.tags.to_a }
video = FactoryBot.create(:video)
subject.videos << video
expect(subject.reload.videos.size).to eql(1)
expect(video.reload.tags.size).to eql(1)
end
it 'can concat records' do
FactoryBot.create(:video, tag_ids: [subject.id])
expect(subject.videos.size).to be_eql(1)
0 Assignees
None
Assign to
0 Reviewers
Request review from
Labels
0
None
0
None
    Assign labels
  • Manage project labels

Milestone
No milestone
None
None
Time tracking
Lock merge request
Unlocked
participants
Reference:
Source branch: github/fork/yosiat/belongs-to-many-fix-include-on-empty-list

Menu

Explore Projects Groups Snippets