Skip to content

Commit

Permalink
Url amigavel com gem friendly_id
Browse files Browse the repository at this point in the history
  • Loading branch information
Godoy committed Mar 7, 2015
1 parent 4ef40f7 commit c3df3da
Show file tree
Hide file tree
Showing 44 changed files with 185 additions and 9 deletions.
3 changes: 2 additions & 1 deletion Gemfile
Expand Up @@ -43,4 +43,5 @@ gem "rails_admin"
gem "bootstrap-sass", "~> 3.3.3" gem "bootstrap-sass", "~> 3.3.3"


gem "faker" gem "faker"
gem "meta-tags" gem "meta-tags"
gem 'friendly_id', '~> 5.1.0'
3 changes: 3 additions & 0 deletions Gemfile.lock
Expand Up @@ -52,6 +52,8 @@ GEM
i18n (~> 0.5) i18n (~> 0.5)
font-awesome-rails (4.2.0.0) font-awesome-rails (4.2.0.0)
railties (>= 3.2, < 5.0) railties (>= 3.2, < 5.0)
friendly_id (5.1.0)
activerecord (>= 4.0.0)
haml (4.0.6) haml (4.0.6)
tilt tilt
hike (1.2.3) hike (1.2.3)
Expand Down Expand Up @@ -167,6 +169,7 @@ DEPENDENCIES
bootstrap-sass (~> 3.3.3) bootstrap-sass (~> 3.3.3)
coffee-rails (~> 4.0.0) coffee-rails (~> 4.0.0)
faker faker
friendly_id (~> 5.1.0)
jbuilder (~> 2.0) jbuilder (~> 2.0)
jquery-rails jquery-rails
meta-tags meta-tags
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/events_controller.rb
Expand Up @@ -29,6 +29,6 @@ def show


private private
def set_event def set_event
@event = Event.find(params[:id]) @event = Event.friendly.find(params[:id])
end end
end end
2 changes: 1 addition & 1 deletion app/controllers/news_controller.rb
Expand Up @@ -30,6 +30,6 @@ def show


private private
def set_news def set_news
@news = News.find(params[:id]) @news = News.friendly.find(params[:id])
end end
end end
3 changes: 3 additions & 0 deletions app/models/category.rb
@@ -1,4 +1,7 @@
class Category < ActiveRecord::Base class Category < ActiveRecord::Base
has_many :events has_many :events
has_many :news has_many :news

extend FriendlyId
friendly_id :name, use: :slugged
end end
18 changes: 17 additions & 1 deletion app/models/event.rb
Expand Up @@ -3,4 +3,20 @@ class Event < ActiveRecord::Base


has_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png" has_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/ validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
end
extend FriendlyId
friendly_id :my_method_slug, use: :slugged

def my_method_slug
start_date.strftime("%m/%d/%Y")+" - #{title}"
end

rails_admin do
create do
exclude_fields :slug
end
edit do
exclude_fields :slug
end
end
end
12 changes: 12 additions & 0 deletions app/models/news.rb
Expand Up @@ -3,4 +3,16 @@ class News < ActiveRecord::Base


has_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png" has_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/ validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/

extend FriendlyId
friendly_id :title, use: :slugged

rails_admin do
create do
exclude_fields :slug
end
edit do
exclude_fields :slug
end
end
end end
88 changes: 88 additions & 0 deletions config/initializers/friendly_id.rb
@@ -0,0 +1,88 @@
# FriendlyId Global Configuration
#
# Use this to set up shared configuration options for your entire application.
# Any of the configuration options shown here can also be applied to single
# models by passing arguments to the `friendly_id` class method or defining
# methods in your model.
#
# To learn more, check out the guide:
#
# http://norman.github.io/friendly_id/file.Guide.html

FriendlyId.defaults do |config|
# ## Reserved Words
#
# Some words could conflict with Rails's routes when used as slugs, or are
# undesirable to allow as slugs. Edit this list as needed for your app.
config.use :reserved

config.reserved_words = %w(new edit index session login logout users admin
stylesheets assets javascripts images)

# ## Friendly Finders
#
# Uncomment this to use friendly finders in all models. By default, if
# you wish to find a record by its friendly id, you must do:
#
# MyModel.friendly.find('foo')
#
# If you uncomment this, you can do:
#
# MyModel.find('foo')
#
# This is significantly more convenient but may not be appropriate for
# all applications, so you must explicity opt-in to this behavior. You can
# always also configure it on a per-model basis if you prefer.
#
# Something else to consider is that using the :finders addon boosts
# performance because it will avoid Rails-internal code that makes runtime
# calls to `Module.extend`.
#
# config.use :finders
#
# ## Slugs
#
# Most applications will use the :slugged module everywhere. If you wish
# to do so, uncomment the following line.
#
# config.use :slugged
#
# By default, FriendlyId's :slugged addon expects the slug column to be named
# 'slug', but you can change it if you wish.
#
# config.slug_column = 'slug'
#
# When FriendlyId can not generate a unique ID from your base method, it appends
# a UUID, separated by a single dash. You can configure the character used as the
# separator. If you're upgrading from FriendlyId 4, you may wish to replace this
# with two dashes.
#
# config.sequence_separator = '-'
#
# ## Tips and Tricks
#
# ### Controlling when slugs are generated
#
# As of FriendlyId 5.0, new slugs are generated only when the slug field is
# nil, but if you're using a column as your base method can change this
# behavior by overriding the `should_generate_new_friendly_id` method that
# FriendlyId adds to your model. The change below makes FriendlyId 5.0 behave
# more like 4.0.
#
# config.use Module.new {
# def should_generate_new_friendly_id?
# slug.blank? || <your_column_name_here>_changed?
# end
# }
#
# FriendlyId uses Rails's `parameterize` method to generate slugs, but for
# languages that don't use the Roman alphabet, that's not usually sufficient.
# Here we use the Babosa library to transliterate Russian Cyrillic slugs to
# ASCII. If you use this, don't forget to add "babosa" to your Gemfile.
#
# config.use Module.new {
# def normalize_friendly_id(text)
# text.to_slug.normalize! :transliterations => [:russian, :latin]
# end
# }
end
8 changes: 4 additions & 4 deletions config/routes.rb
@@ -1,11 +1,11 @@
Rails.application.routes.draw do Rails.application.routes.draw do
root 'pages#home' root 'pages#home'


get 'events' => 'events#index', as: :events get 'eventos' => 'events#index', as: :events
get 'events/:id' => 'events#show', as: :event get 'eventos/:id' => 'events#show', as: :event


get 'news' => 'news#index', as: :news_index get 'noticias' => 'news#index', as: :news_index
get 'news/:id' => 'news#show', as: :news get 'noticias/:id' => 'news#show', as: :news


mount RailsAdmin::Engine => '/admin', as: 'rails_admin' mount RailsAdmin::Engine => '/admin', as: 'rails_admin'
end end
15 changes: 15 additions & 0 deletions db/migrate/20150306235422_create_friendly_id_slugs.rb
@@ -0,0 +1,15 @@
class CreateFriendlyIdSlugs < ActiveRecord::Migration
def change
create_table :friendly_id_slugs do |t|
t.string :slug, :null => false
t.integer :sluggable_id, :null => false
t.string :sluggable_type, :limit => 50
t.string :scope
t.datetime :created_at
end
add_index :friendly_id_slugs, :sluggable_id
add_index :friendly_id_slugs, [:slug, :sluggable_type]
add_index :friendly_id_slugs, [:slug, :sluggable_type, :scope], :unique => true
add_index :friendly_id_slugs, :sluggable_type
end
end
6 changes: 6 additions & 0 deletions db/migrate/20150306235728_add_slug_to_news.rb
@@ -0,0 +1,6 @@
class AddSlugToNews < ActiveRecord::Migration
def change
add_column :news, :slug, :string
add_index :news, :slug, unique: true
end
end
6 changes: 6 additions & 0 deletions db/migrate/20150306235746_add_slug_to_events.rb
@@ -0,0 +1,6 @@
class AddSlugToEvents < ActiveRecord::Migration
def change
add_column :events, :slug, :string
add_index :events, :slug, unique: true
end
end
6 changes: 6 additions & 0 deletions db/migrate/20150306235926_add_slug_to_categories.rb
@@ -0,0 +1,6 @@
class AddSlugToCategories < ActiveRecord::Migration
def change
add_column :categories, :slug, :string
add_index :categories, :slug, unique: true
end
end
22 changes: 21 additions & 1 deletion db/schema.rb
Expand Up @@ -11,14 +11,17 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.


ActiveRecord::Schema.define(version: 20150222204612) do ActiveRecord::Schema.define(version: 20150306235926) do


create_table "categories", force: true do |t| create_table "categories", force: true do |t|
t.string "name" t.string "name"
t.datetime "created_at" t.datetime "created_at"
t.datetime "updated_at" t.datetime "updated_at"
t.string "slug"
end end


add_index "categories", ["slug"], name: "index_categories_on_slug", unique: true

create_table "events", force: true do |t| create_table "events", force: true do |t|
t.string "title" t.string "title"
t.integer "category_id" t.integer "category_id"
Expand All @@ -32,9 +35,24 @@
t.string "place" t.string "place"
t.datetime "created_at" t.datetime "created_at"
t.datetime "updated_at" t.datetime "updated_at"
t.string "slug"
end end


add_index "events", ["category_id"], name: "index_events_on_category_id" add_index "events", ["category_id"], name: "index_events_on_category_id"
add_index "events", ["slug"], name: "index_events_on_slug", unique: true

create_table "friendly_id_slugs", force: true do |t|
t.string "slug", null: false
t.integer "sluggable_id", null: false
t.string "sluggable_type", limit: 50
t.string "scope"
t.datetime "created_at"
end

add_index "friendly_id_slugs", ["slug", "sluggable_type", "scope"], name: "index_friendly_id_slugs_on_slug_and_sluggable_type_and_scope", unique: true
add_index "friendly_id_slugs", ["slug", "sluggable_type"], name: "index_friendly_id_slugs_on_slug_and_sluggable_type"
add_index "friendly_id_slugs", ["sluggable_id"], name: "index_friendly_id_slugs_on_sluggable_id"
add_index "friendly_id_slugs", ["sluggable_type"], name: "index_friendly_id_slugs_on_sluggable_type"


create_table "news", force: true do |t| create_table "news", force: true do |t|
t.string "title" t.string "title"
Expand All @@ -46,8 +64,10 @@
t.datetime "image_updated_at" t.datetime "image_updated_at"
t.datetime "created_at" t.datetime "created_at"
t.datetime "updated_at" t.datetime "updated_at"
t.string "slug"
end end


add_index "news", ["category_id"], name: "index_news_on_category_id" add_index "news", ["category_id"], name: "index_news_on_category_id"
add_index "news", ["slug"], name: "index_news_on_slug", unique: true


end end
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit c3df3da

Please sign in to comment.