### Setup Development Environment Source: https://github.com/rameerez/slugifiable/blob/main/README.md Installs dependencies and sets up the development environment for the slugifiable gem. Allows for interactive experimentation via the console. ```bash bin/setup rake spec bin/console ``` -------------------------------- ### Basic Slug Generation Example Source: https://github.com/rameerez/slugifiable/blob/main/README.md Demonstrates how to include the Slugifiable::Model module in a Rails model to enable basic slug generation. This is the initial setup required to use the gem. ```ruby class Product < ApplicationRecord include Slugifiable::Model # Adding this provides all the required slug-related methods to your model end ``` -------------------------------- ### Install Gem Locally Source: https://github.com/rameerez/slugifiable/blob/main/README.md Installs the slugifiable gem onto your local machine after cloning the repository. This command makes the gem available for local development and testing. ```bash bundle exec rake install ``` -------------------------------- ### Example Model with Method-Based Slug Generation Source: https://github.com/rameerez/slugifiable/blob/main/README.md Demonstrates a model including Slugifiable::Model and defining a custom method 'title_with_location' for slug generation. This method combines title, city, and region for a more descriptive slug. ```ruby class Event < ApplicationRecord include Slugifiable::Model belongs_to :location generate_slug_based_on :title_with_location # The method can return any string - slugifiable will handle the parameterization def title_with_location if location.present? "#{title} #{location.city} #{location.region}" # Returns raw string, slugifiable parameterizes it else title end end end ``` -------------------------------- ### Run Gem Tests Source: https://github.com/rameerez/slugifiable/blob/main/README.md Installs dependencies and runs the comprehensive test suite for the slugifiable gem using Rake. The tests cover various slug generation scenarios and collision resolution. ```bash bundle install bundle exec rake test ``` -------------------------------- ### Non-Nullable Slug Column Setup with Callback Source: https://github.com/rameerez/slugifiable/blob/main/README.md Configures a non-nullable 'slug' column, requiring a `before_validation` callback to ensure a slug is always present upon creation. This setup includes automatic race condition handling. ```ruby # migration add_column :products, :slug, :string, null: false add_index :products, :slug, unique: true # model class Product < ApplicationRecord include Slugifiable::Model generate_slug_based_on :name before_validation :ensure_slug_present, on: :create private def ensure_slug_present self.slug = compute_slug if slug.blank? end end ``` -------------------------------- ### Configure Slug Generation Based on Instance Method Source: https://github.com/rameerez/slugifiable/blob/main/README.md Generates slugs by calling a specified instance method. The method can combine multiple attributes or perform complex logic. The returned string is automatically parameterized. ```ruby generate_slug_based_on :title_with_location ``` -------------------------------- ### Nullable Slug Column Migration and Model Source: https://github.com/rameerez/slugifiable/blob/main/README.md Sets up a nullable 'slug' column in the database and configures the model to use it. This is the default and simpler approach for slug management. ```ruby # migration add_column :products, :slug, :string add_index :products, :slug, unique: true # model class Product < ApplicationRecord include Slugifiable::Model generate_slug_based_on :name end ``` -------------------------------- ### Generate Migration for Slug Attribute Source: https://github.com/rameerez/slugifiable/blob/main/README.md Run this command to generate a migration to add a 'slug' attribute to your model. Replace with your model's plural name. ```bash rails g migration addSlugTo slug:text ``` -------------------------------- ### Configure Default Slug Generation (Hex String) Source: https://github.com/rameerez/slugifiable/blob/main/README.md Sets the default slug generation to use a SHA hash of the record ID, formatted as a hex string. This is the default behavior. ```ruby generate_slug_based_on id: :hex_string ``` -------------------------------- ### Configure Default Slug Generation (Number) Source: https://github.com/rameerez/slugifiable/blob/main/README.md Configures slug generation to use a nonconsecutive, nonincremental number based on the record ID. This provides obfuscated, number-only slugs. ```ruby generate_slug_based_on id: :number ``` -------------------------------- ### Generate Slug Based on Name Source: https://github.com/rameerez/slugifiable/blob/main/README.md Configures the model to generate slugs based on the 'name' attribute. This is useful for creating human-readable and SEO-friendly slugs. ```ruby class Product < ApplicationRecord include Slugifiable::Model generate_slug_based_on :name end ``` -------------------------------- ### Configure Slug Generation Based on Attribute Source: https://github.com/rameerez/slugifiable/blob/main/README.md Generates slugs based on the value of a specified attribute (e.g., ':name'). The gem automatically parameterizes the attribute's value. ```ruby generate_slug_based_on :name ``` -------------------------------- ### Configure Slug Length for Obfuscated Slugs Source: https://github.com/rameerez/slugifiable/blob/main/README.md Specifies the desired length for obfuscated slugs (hex string or number based on ID). The length must be between 1 and 64. ```ruby generate_slug_based_on id: :number, length: 3 ``` -------------------------------- ### Generate Fixed-Length ID-Based Slug Source: https://github.com/rameerez/slugifiable/blob/main/README.md Generates slugs based on the model's 'id' attribute with a specified fixed length. This is useful for creating shorter, predictable slugs. ```ruby class Product < ApplicationRecord include Slugifiable::Model generate_slug_based_on :id, length: 6 end ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.