Skip to content
This repository was archived by the owner on Oct 19, 2018. It is now read-only.
This repository was archived by the owner on Oct 19, 2018. It is now read-only.

add #build / #new and make << / #push save like active record does #63

Open
@catmando

Description

@catmando

<< should save, and build should not.

change push's name to build (be careful push / << may be used internally in Hypermesh) and add a new push which does build(...).save and returns self.

only allow build on root collections (not scopes) as the scope behavior is to fill in the attributes to match the scope. (see #64)

have to add the old behavior back into the deprecation gem.

Activity

added this to the Release 0.15 milestone on Feb 9, 2018
changed the title [-]add `#build` and make `<< / #push` act like active record[/-] [+]add `#build / #new` and make `<< / #push` save like active record does[/+] on Feb 9, 2018
sfcgeorge

sfcgeorge commented on Feb 9, 2018

@sfcgeorge
Contributor

Patch for build - just extending current << behaviour to instantiate the model:

module ReactiveRecord
  class Collection
    unless method_defined?(:build)
      def build(**attrs)
        self << @association.klass.new(**attrs)
      end
    end
  end
end

Sketch of other methods, but I'm not sure the behaviour matches Rails, the docs aren't clear enough so some experimentation and looking at Rails code needed.

def <<(other)
  # push is current alias of <<
  push(other).save
end

def create(**attrs)
  build(**attrs)
  @owner.save
end

def create!(**attrs)
  build(**attrs)
  @owner.save!
end
catmando

catmando commented on Feb 9, 2018

@catmando
ContributorAuthor

push and << are aliases
so it would have to be

def <<(other)
  build(other).tap { |r| r.save }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @catmando@sfcgeorge

        Issue actions

          add `#build / #new` and make `<< / #push` save like active record does · Issue #63 · ruby-hyperloop/hyper-mesh