krisquigley / slugfest-poc Goto Github PK
View Code? Open in Web Editor NEWA Proof of Concept for a central slug repository in Rails
A Proof of Concept for a central slug repository in Rails
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 changesGeneral
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 of active
is masked from the specL37, L42, L47, L52, L57
L2, L6
update_active_slug_prefixes
to be more explicit with intentL7
General
active
and inactive
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
L4, as per app/models/admin.rb L7 above, and L34 below
slug_prefix
to argumentsslug_computer
to arguments, with a default of Slugs::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 itactive_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
Slugs::ComputeSlug.call
with slug_computer.call
to allow for dependency injection in your specsresource_type
with passing slug_prefix
from this class (as we pass in as an argument)L2, L6, L8
resource_type
with slug_prefix
, both places this service is implemented, we know where the prefix comes fromL25
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 the Slugs::ComputeSlug
service used in L62L47
transaction
is an instance method on AR models, not just a class method so you can replace self.class.transaction do
with just transaction do
L61
computed_slug
L62
resource_type
with slug_prefix
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
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.