Permalink
Browse files

Url amigavel com gem friendly_id

  • Loading branch information...
Godoy committed Mar 7, 2015
1 parent 4ef40f7 commit c3df3da6377ee6ebc094de8f09f83c8a154d48cd
Showing with 185 additions and 9 deletions.
  1. +2 −1 Gemfile
  2. +3 −0 Gemfile.lock
  3. +1 −1 app/controllers/events_controller.rb
  4. +1 −1 app/controllers/news_controller.rb
  5. +3 −0 app/models/category.rb
  6. +17 −1 app/models/event.rb
  7. +12 −0 app/models/news.rb
  8. +88 −0 config/initializers/friendly_id.rb
  9. +4 −4 config/routes.rb
  10. +15 −0 db/migrate/20150306235422_create_friendly_id_slugs.rb
  11. +6 −0 db/migrate/20150306235728_add_slug_to_news.rb
  12. +6 −0 db/migrate/20150306235746_add_slug_to_events.rb
  13. +6 −0 db/migrate/20150306235926_add_slug_to_categories.rb
  14. +21 −1 db/schema.rb
  15. BIN public/system/events/images/000/000/001/medium/rationecupiditatevoluptatem.png
  16. BIN public/system/events/images/000/000/001/original/rationecupiditatevoluptatem.png
  17. BIN public/system/events/images/000/000/001/thumb/rationecupiditatevoluptatem.png
  18. BIN public/system/events/images/000/000/002/medium/beataevoluptatemat.png
  19. BIN public/system/events/images/000/000/002/original/beataevoluptatemat.png
  20. BIN public/system/events/images/000/000/002/thumb/beataevoluptatemat.png
  21. BIN public/system/events/images/000/000/003/medium/harumnesciuntveritatis.png
  22. BIN public/system/events/images/000/000/003/original/harumnesciuntveritatis.png
  23. BIN public/system/events/images/000/000/003/thumb/harumnesciuntveritatis.png
  24. BIN public/system/events/images/000/000/004/medium/autquodquasi.png
  25. BIN public/system/events/images/000/000/004/original/autquodquasi.png
  26. BIN public/system/events/images/000/000/004/thumb/autquodquasi.png
  27. BIN public/system/events/images/000/000/005/medium/quiasunttemporibus.png
  28. BIN public/system/events/images/000/000/005/original/quiasunttemporibus.png
  29. BIN public/system/events/images/000/000/005/thumb/quiasunttemporibus.png
  30. BIN public/system/news/images/000/000/001/medium/024.gif
  31. BIN public/system/news/images/000/000/001/original/024.gif
  32. BIN public/system/news/images/000/000/001/thumb/024.gif
  33. BIN public/system/news/images/000/000/002/medium/004.gif
  34. BIN public/system/news/images/000/000/002/original/004.gif
  35. BIN public/system/news/images/000/000/002/thumb/004.gif
  36. BIN public/system/news/images/000/000/003/medium/002.gif
  37. BIN public/system/news/images/000/000/003/original/002.gif
  38. BIN public/system/news/images/000/000/003/thumb/002.gif
  39. BIN public/system/news/images/000/000/004/medium/038.gif
  40. BIN public/system/news/images/000/000/004/original/038.gif
  41. BIN public/system/news/images/000/000/004/thumb/038.gif
  42. BIN public/system/news/images/000/000/005/medium/067.gif
  43. BIN public/system/news/images/000/000/005/original/067.gif
  44. BIN public/system/news/images/000/000/005/thumb/067.gif
View
@@ -43,4 +43,5 @@ gem "rails_admin"
gem "bootstrap-sass", "~> 3.3.3"
gem "faker"
gem "meta-tags"
gem "meta-tags"
gem 'friendly_id', '~> 5.1.0'
View
@@ -52,6 +52,8 @@ GEM
i18n (~> 0.5)
font-awesome-rails (4.2.0.0)
railties (>= 3.2, < 5.0)
friendly_id (5.1.0)
activerecord (>= 4.0.0)
haml (4.0.6)
tilt
hike (1.2.3)
@@ -167,6 +169,7 @@ DEPENDENCIES
bootstrap-sass (~> 3.3.3)
coffee-rails (~> 4.0.0)
faker
friendly_id (~> 5.1.0)
jbuilder (~> 2.0)
jquery-rails
meta-tags
@@ -29,6 +29,6 @@ def show
private
def set_event
@event = Event.find(params[:id])
@event = Event.friendly.find(params[:id])
end
end
@@ -30,6 +30,6 @@ def show
private
def set_news
@news = News.find(params[:id])
@news = News.friendly.find(params[:id])
end
end
View
@@ -1,4 +1,7 @@
class Category < ActiveRecord::Base
has_many :events
has_many :news
extend FriendlyId
friendly_id :name, use: :slugged
end
View
@@ -3,4 +3,20 @@ class Event < ActiveRecord::Base
has_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
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
View
@@ -3,4 +3,16 @@ class News < ActiveRecord::Base
has_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
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
@@ -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
View
@@ -1,11 +1,11 @@
Rails.application.routes.draw do
root 'pages#home'
get 'events' => 'events#index', as: :events
get 'events/:id' => 'events#show', as: :event
get 'eventos' => 'events#index', as: :events
get 'eventos/:id' => 'events#show', as: :event
get 'news' => 'news#index', as: :news_index
get 'news/:id' => 'news#show', as: :news
get 'noticias' => 'news#index', as: :news_index
get 'noticias/:id' => 'news#show', as: :news
mount RailsAdmin::Engine => '/admin', as: 'rails_admin'
end
@@ -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
@@ -0,0 +1,6 @@
class AddSlugToNews < ActiveRecord::Migration
def change
add_column :news, :slug, :string
add_index :news, :slug, unique: true
end
end
@@ -0,0 +1,6 @@
class AddSlugToEvents < ActiveRecord::Migration
def change
add_column :events, :slug, :string
add_index :events, :slug, unique: true
end
end
@@ -0,0 +1,6 @@
class AddSlugToCategories < ActiveRecord::Migration
def change
add_column :categories, :slug, :string
add_index :categories, :slug, unique: true
end
end
View
@@ -11,14 +11,17 @@
#
# 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|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
t.string "slug"
end
add_index "categories", ["slug"], name: "index_categories_on_slug", unique: true
create_table "events", force: true do |t|
t.string "title"
t.integer "category_id"
@@ -32,9 +35,24 @@
t.string "place"
t.datetime "created_at"
t.datetime "updated_at"
t.string "slug"
end
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|
t.string "title"
@@ -46,8 +64,10 @@
t.datetime "image_updated_at"
t.datetime "created_at"
t.datetime "updated_at"
t.string "slug"
end
add_index "news", ["category_id"], name: "index_news_on_category_id"
add_index "news", ["slug"], name: "index_news_on_slug", unique: true
end
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.

0 comments on commit c3df3da

Please sign in to comment.