GithubHelp home page GithubHelp logo

Comments (3)

gee-forr avatar gee-forr commented on June 30, 2024 1

OMG! This is amazing! Thank you so much!

from clowne.

ssnickolay avatar ssnickolay commented on June 30, 2024

Hey @gee-forr .
Hm, you have an interesting case...AFAIU you could try to do something like this:

class Course < ApplicationRecord
  has_many :course_modules

  # other associations
end
 
class CourseSnapshot < Course
  self.table_name = 'course_snapshots'
  
  # foreign key to keep tables equal between each other
  has_many :course_modules, class_name: 'CourseModuleSnapshot', foreign_key: :course_id
  # other associations with overrided class_name & foreign_key options
end

class CourseModule < ApplicationRecord; end

class CourseModuleSnapshot < CourseModule
  self.table_name = 'course_module_snapshots'
end

# Magic here:
class SnapshotAdapter < Clowne::Adapters::ActiveRecord
  class << self 
    def dup_record(record)
      # we cannot mix snapshot and origin models because of https://api.rubyonrails.org/classes/ActiveRecord/AssociationTypeMismatch.html
      # so we override dup logic to always have snap models
      # if you have witelisted models that should be cloned as is 
      # you can `return super if %w(SomeModel).include?(record.class)`
      target_class = "#{record.class.name}Snapshot".constantize

      target_class.new(record.dup.attributes).tap do |clone|
        # you can skip these lines if you dont use after_persist hook (see https://clowne.evilmartians.io/#/after_persist)
        operation = operation_class.current
        operation.add_mapping(record, clone)
      end
    end
  end
end

class CourseSnapshoter < BasePostCloner
  adapter SnapshotAdapter # override default adapter

  include_association :course_modules
end

# and use

CourseSnapshoter.call(Course.last).to_record
# => <# CourseSnapshote ... >

I checked locally with a similar example and everything seems to work

from clowne.

bugthing avatar bugthing commented on June 30, 2024

@ssnickolay thanks answering this 2 years back 😆 .. it has partially addressed a problem I am having trying to use clowne.
When I clone an AR model into a different one (as demoed above), I also need to rename an association.. is this possible?

from clowne.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.