krisquigley / slugfest-poc

A Proof of Concept for a central slug repository in Rails
1 stars 0 forks source link

Feedback Round 1 #1

Open ryantownsend opened 8 years ago

ryantownsend commented 8 years ago

spec/models/admin_spec.rb

General

L9-L11

L15-31

General

L11-14

L17, L23, L29

L93, L99, L104, as per app/models/slug.rb below

L37, L42, L47, L52, L57

L2, L6

L7

General

L2

L4, as per app/models/admin.rb L7 above, and L34 below

L14,L15

L25

active_slug.dup.tap { |s| s.assign_attributes(slug_params(s)) }

You will just need to add active: true to slug_params. Then L17-L18 becomes:

generate_record(active_slug).save!

L34, as per L4 above and app/services/slugs/compute_slug.rb L2/L6/L8 below

L2, L6, L8

L25

L43

L46-L56

L47

L61

L62

So ultimately, compute_slug is removed and update_slug_history becomes:

after_save :update_slug_history # this still allows for rolling-back the transaction

def update_slug_history
  unless Slugs::UpdateSluggableHistory.call(slug_prefix: self.class.slug_prefix, resource: self)
    self.errors.add(:slug, 'already taken')
    throw :abort # Rails 5 only
    raise ActiveRecord::RecordInvalid # Rails 4, or may be: raise ActiveRecord::Rollback
  end
end

def computed_slug # note: no longer compute_slug
  Slugs::ComputeSlug.call(slug_prefix: slug_prefix, slug: resource.slug)
end

class Slugs::UpdateSluggableHistory
  def self.call(*args)
    new(*args).call
  end

  def initialize(slug_prefix:, resource:)
    @slug_prefix = slug_prefix
    @resource = resource
  end

  def call
    @resource.slugs.active.update_all(active: false) &&
    @resource.slugs.active.create(
      slug_prefix: @slug_prefix, 
      slug: @resource.slug, 
      computed_slug: @resource.computed_slug
    )
  end
end
krisquigley commented 8 years ago

Looking through this again, my previous concerns were not relevant. Should have this done today.

krisquigley commented 8 years ago

@resource.slugs.active.create( slug_prefix: @slug_prefix, slug: @resource.slug, computed_slug: @resource.computed_slug )

Unable to call #create here as the parent hasn't been saved yet, reverted to build