Open ryantownsend opened 8 years ago
Looking through this again, my previous concerns were not relevant. Should have this done today.
@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
spec/models/admin_spec.rb
General
L9-L11
create_list(:product, 2)
L15-31
aggregate_failures
block, means it'll output all failures, not just one spec spec then stop, better for debugging a collection of changesspec/models/product_spec.rb
General
sluggable_spec.rb
Sluggable
module is included, e.g.it_behaves_like 'a sluggable model'
. This way 1) we've centralised the spec against the relevant module and 2) the model specs are rapidL11-14
L17, L23, L29
L93, L99, L104, as per app/models/slug.rb below
subject.slugs.active.first
, means the implementation ofactive
is masked from the specL37, L42, L47, L52, L57
app/models/admin.rb
L2, L6
update_active_slug_prefixes
to be more explicit with intentL7
app/models/slug.rb
General
active
andinactive
named-scopesself.active_for_resource_type(resource_type); active.where(resource_type: resource_type); end
method, this treats the AR model as more of a repository rather than chaining AR scopes outside the class too muchL2
app/services/slugs/update_slug_prefixes_for_active_slugs.rb
L4, as per app/models/admin.rb L7 above, and L34 below
slug_prefix
to argumentsslug_computer
to arguments, with a default ofSlugs::ComputeSlug
to allow for dependency injection in your specsL14,L15
Slug.active_for_resource_type(resource_type).find_in_batches do |batch| batch.each do |slug| ...
this will lower memory usageL25
generate_attributes
would be more consistent with the output, however if we reimplement as below, then it will be generating a record, so we can leave itYou will just need to add
active: true
toslug_params
. Then L17-L18 becomes:L34, as per L4 above and app/services/slugs/compute_slug.rb L2/L6/L8 below
Slugs::ComputeSlug.call
withslug_computer.call
to allow for dependency injection in your specsresource_type
with passingslug_prefix
from this class (as we pass in as an argument)app/services/slugs/compute_slug.rb
L2, L6, L8
resource_type
withslug_prefix
, both places this service is implemented, we know where the prefix comes fromapp/models/concerns/sluggable.rb
L25
after_save
can be combined with a rollback to give us what we want in a cleaner way (not needing to use .build for the new slug)L43
active
named scopeL46-L56
Slugs::UpdateSluggableHistory
, this means we can use dependency injection to pass in theSlugs::ComputeSlug
service used in L62L47
transaction
is an instance method on AR models, not just a class method so you can replaceself.class.transaction do
with justtransaction do
L61
computed_slug
L62
resource_type
withslug_prefix
So ultimately,
compute_slug
is removed andupdate_slug_history
becomes: