Skip to content
Browse files

merging...

  • Loading branch information...
2 parents 96986b4 + 7855caa commit 31e228092766d94fc8ef495f774e977086d8e8cc @juanignaciosl juanignaciosl committed Apr 23, 2012
View
13 Gemfile
@@ -1,6 +1,6 @@
source 'https://rubygems.org'
-gem 'rails', '~> 3.2.2'
+gem 'rails', '~> 3.2.3'
gem 'rails-i18n', '~> 0.6.1'
# Bundle edge Rails instead:
@@ -26,7 +26,7 @@ gem 'jquery-rails', '~> 2.0.1'
# gem 'jbuilder'
# Use unicorn as the app server
-gem 'unicorn', '~> 4.2.0'
+gem 'unicorn', '~> 4.3.0'
# Deploy with Capistrano
# gem 'capistrano'
@@ -40,9 +40,9 @@ gem 'unicorn', '~> 4.2.0'
# gem 'ruby-debug19', :require => 'ruby-debug'
group :development, :test do
- gem 'factory_girl_rails', '~> 3.0.0'
+ gem 'factory_girl_rails', '~> 3.1'
gem 'ffaker', '~> 1.14.0'
- gem 'mocha', '~> 0.10.5', :require => false
+ gem 'mocha', '~> 0.11.1', :require => false
end
# pagination
@@ -53,8 +53,9 @@ gem 'devise', '~> 2.0.4'
# gem 'cancan'
# upload files
gem 'mini_magick', '~> 3.4'
-gem 'carrierwave', '~> 0.5.8'
+gem 'carrierwave', '~> 0.6.2'
# better forms
gem 'simple_form', '~> 2.0.1'
-gem 'draper'
+# better urls
+gem 'friendly_id', '~> 4.0.4'
View
14 app/controllers/mgzine_controller.rb.orig
@@ -0,0 +1,14 @@
+# -*- encoding: utf-8 -*-
+
+class MgzineController < ApplicationController
+
+ def index
+<<<<<<< HEAD
+ @posts = PostDecorator.all
+
+ @events = EventDecorator.decorate Event.next_events
+=======
+ @posts = Post.all
+>>>>>>> master
+ end
+end
View
15 app/controllers/posts_controller.rb
@@ -1,11 +1,24 @@
class PostsController < ApplicationController
before_filter :find_post
+ before_filter :post_moved_permanently, only: [:show]
def find_post
- @post = PostDecorator.find params[:id]
+ @post = Post.find params[:id]
end
protected :find_post
+ def post_moved_permanently
+ # from the friendly_id documentation:
+ #
+ # If an old id or a numeric id was used to find the record, then
+ # the request path will not match the post_path, and we should do
+ # a 301 redirect that uses the current friendly id.
+ if request.path != post_path(@post)
+ redirect_to @post, status: :moved_permanently
+ end
+ end
+ protected :post_moved_permanently
+
# GET /posts/1
# GET /posts/1.json
def show
View
28 app/decorators/application_decorator.rb
@@ -1,28 +0,0 @@
-class ApplicationDecorator < Draper::Base
- # Lazy Helpers
- # PRO: Call Rails helpers without the h. proxy
- # ex: number_to_currency(model.price)
- # CON: Add a bazillion methods into your decorator's namespace
- # and probably sacrifice performance/memory
- #
- # Enable them by uncommenting this line:
- # lazy_helpers
-
- # Shared Decorations
- # Consider defining shared methods common to all your models.
- #
- # Example: standardize the formatting of timestamps
- #
- # def formatted_timestamp(time)
- # h.content_tag :span, time.strftime("%a %m/%d/%y"),
- # :class => 'timestamp'
- # end
- #
- # def created_at
- # formatted_timestamp(model.created_at)
- # end
- #
- # def updated_at
- # formatted_timestamp(model.updated_at)
- # end
-end
View
48 app/decorators/post_decorator.rb
@@ -1,48 +0,0 @@
-class PostDecorator < ApplicationDecorator
- decorates :post
-
- def linked_title
- h.link_to model.title, model;
- end
-
- def excerpt
- if post.excerpt.present?
- h.render partial: 'posts/excerpt', locals: { excerpt: post.excerpt }
- end
- end
-
- def category
- if post.category.present?
- h.render partial: 'posts/category', locals: { category: post.category }
- end
- end
-
- # Accessing Helpers
- # You can access any helper via a proxy
- #
- # Normal Usage: helpers.number_to_currency(2)
- # Abbreviated : h.number_to_currency(2)
- #
- # Or, optionally enable "lazy helpers" by including this module:
- # include Draper::LazyHelpers
- # Then use the helpers with no proxy:
- # number_to_currency(2)
-
- # Defining an Interface
- # Control access to the wrapped subject's methods using one of the following:
- #
- # To allow only the listed methods (whitelist):
- # allows :method1, :method2
- #
- # To allow everything except the listed methods (blacklist):
- # denies :method1, :method2
-
- # Presentation Methods
- # Define your own instance methods, even overriding accessors
- # generated by ActiveRecord:
- #
- # def created_at
- # h.content_tag :span, time.strftime("%a %m/%d/%y"),
- # :class => 'timestamp'
- # end
-end
View
10 app/helpers/application_helper.rb
@@ -52,11 +52,11 @@ def render_shared(partial, _scope = nil)
end
# TODO: add namespaces for presenters
- # def present(object, klass = nil)
- # presenter = BasePresenter.build(object, self, klass)
+ def present(object, klass = nil)
+ presenter = BasePresenter.build(object, self, klass)
- # yield presenter if block_given?
+ yield presenter if block_given?
- # presenter
- # end
+ presenter
+ end
end
View
5 app/models/category.rb
@@ -1,4 +1,9 @@
+# -*- encoding: utf-8 -*-
+
class Category < ActiveRecord::Base
+ extend FriendlyId
+ friendly_id :name, use: :slugged
+
validates :name, uniqueness: true, presence: true
attr_accessible :name
View
3 app/models/post.rb
@@ -1,7 +1,8 @@
# -*- encoding: utf-8 -*-
-# TODO: tests!
class Post < ActiveRecord::Base
+ extend FriendlyId
+ friendly_id :title, use: [:slugged, :history]
validates :title, :category, presence: true
View
18 app/presenters/post_presenter.rb
@@ -1,7 +1,23 @@
# -*- encoding: utf-8 -*-
-# TODO: use it!
# TODO: tests!
class PostPresenter < BasePresenter
presents :post
+ delegate :title, :published_at, :body, :images, to: :post
+
+ def linked_title
+ h.link_to post.title, post;
+ end
+
+ def excerpt
+ if post.excerpt.present?
+ h.render partial: 'posts/excerpt', locals: { excerpt: post.excerpt }
+ end
+ end
+
+ def category
+ if post.category.present?
+ h.render partial: 'posts/category', locals: { category: post.category }
+ end
+ end
end
View
4 app/views/mgzine/index.html.erb
@@ -1,10 +1,10 @@
<%- title 'NTD Mgzine' -%>
-<% @posts.each do |post| %>
+<% @posts.map { |p| present p }.each do |post| %>
<section>
<h1><%= post.linked_title %></h1>
<%= post.category %>
-</section>
+</section>
<% end %>
<h1>Agenda</h1>
View
66 app/views/posts/show.html.erb
@@ -1,35 +1,31 @@
-<%- title "#{@post.title}" -%>
-
-<article>
-
- <header><h1><%= @post.title %></h1></header>
-
- <time><%= l @post.published_at, format: :long %></time>
-
- <%= @post.excerpt %>
-
- <section class="body">
- <%= @post.body %>
- </section>
-
- <%=
- if @post.event.present?
- render @post.event
- end
- %>
-
-
- <!-- TODO -->
-
- <ul class="thumbnails">
- <% @post.images.each do |image| %>
- <li class="span3">
- <div class="thumbnail">
- <%= image_tag image.resource_url(:thumb).to_s %>
- <h5><%= image.title %></h5>
- </div>
- </li>
- <% end -%>
- </ul>
-
-</article>
+<% present @post do |presenter| -%>
+ <%- title "#{presenter.title}" -%>
+
+ <article>
+
+ <header><h1><%= presenter.title %></h1></header>
+
+ <time><%= l presenter.published_at, format: :long %></time>
+
+ <%= presenter.excerpt %>
+
+ <section class="body">
+ <%= presenter.body %>
+ </section>
+
+
+ <!-- TODO -->
+
+ <ul class="thumbnails">
+ <% presenter.images.each do |image| %>
+ <li class="span3">
+ <div class="thumbnail">
+ <%= image_tag image.resource_url(:thumb).to_s %>
+ <h5><%= image.title %></h5>
+ </div>
+ </li>
+ <% end -%>
+ </ul>
+
+ </article>
+<% end -%>
View
69 app/views/posts/show.html.erb.orig
@@ -0,0 +1,69 @@
+<<<<<<< HEAD
+<%- title "#{@post.title}" -%>
+
+<article>
+
+ <header><h1><%= @post.title %></h1></header>
+
+ <time><%= l @post.published_at, format: :long %></time>
+
+ <%= @post.excerpt %>
+
+ <section class="body">
+ <%= @post.body %>
+ </section>
+
+ <%=
+ if @post.event.present?
+ render @post.event
+ end
+ %>
+
+
+ <!-- TODO -->
+
+ <ul class="thumbnails">
+ <% @post.images.each do |image| %>
+ <li class="span3">
+ <div class="thumbnail">
+ <%= image_tag image.resource_url(:thumb).to_s %>
+ <h5><%= image.title %></h5>
+ </div>
+ </li>
+ <% end -%>
+ </ul>
+
+</article>
+=======
+<% present @post do |presenter| -%>
+ <%- title "#{presenter.title}" -%>
+
+ <article>
+
+ <header><h1><%= presenter.title %></h1></header>
+
+ <time><%= l presenter.published_at, format: :long %></time>
+
+ <%= presenter.excerpt %>
+
+ <section class="body">
+ <%= presenter.body %>
+ </section>
+
+
+ <!-- TODO -->
+
+ <ul class="thumbnails">
+ <% presenter.images.each do |image| %>
+ <li class="span3">
+ <div class="thumbnail">
+ <%= image_tag image.resource_url(:thumb).to_s %>
+ <h5><%= image.title %></h5>
+ </div>
+ </li>
+ <% end -%>
+ </ul>
+
+ </article>
+<% end -%>
+>>>>>>> master
View
6 db/migrate/20120420184536_add_key_to_categories.rb
@@ -0,0 +1,6 @@
+class AddKeyToCategories < ActiveRecord::Migration
+ def change
+ add_column :categories, :key, :string
+ add_index :categories, :key
+ end
+end
View
6 db/migrate/20120421110308_add_slug_to_posts.rb
@@ -0,0 +1,6 @@
+class AddSlugToPosts < ActiveRecord::Migration
+ def change
+ add_column :posts, :slug, :string
+ add_index :posts, :slug
+ end
+end
View
18 db/migrate/20120421110715_create_friendly_id_slugs.rb
@@ -0,0 +1,18 @@
+class CreateFriendlyIdSlugs < ActiveRecord::Migration
+
+ def self.up
+ create_table :friendly_id_slugs do |t|
+ t.string :slug, :null => false
+ t.integer :sluggable_id, :null => false
+ t.string :sluggable_type, :limit => 40
+ t.datetime :created_at
+ end
+ add_index :friendly_id_slugs, :sluggable_id
+ add_index :friendly_id_slugs, [:slug, :sluggable_type], :unique => true
+ add_index :friendly_id_slugs, :sluggable_type
+ end
+
+ def self.down
+ drop_table :friendly_id_slugs
+ end
+end
View
7 db/migrate/20120421113622_rename_key_to_slug_in_categories.rb
@@ -0,0 +1,7 @@
+class RenameKeyToSlugInCategories < ActiveRecord::Migration
+ def change
+ rename_column :categories, :key, :slug
+ remove_index :categories, :key
+ add_index :categories, :slug
+ end
+end
View
19 db/schema.rb
@@ -11,12 +11,13 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20120418224035) do
+ActiveRecord::Schema.define(:version => 20120421113622) do
create_table "categories", :force => true do |t|
t.string "name"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
+ t.string "slug"
end
create_table "events", :force => true do |t|
@@ -29,6 +30,19 @@
t.integer "post_id"
end
+ add_index "categories", ["slug"], :name => "index_categories_on_slug"
+
+ 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 => 40
+ t.datetime "created_at"
+ end
+
+ add_index "friendly_id_slugs", ["slug", "sluggable_type"], :name => "index_friendly_id_slugs_on_slug_and_sluggable_type", :unique => true
+ 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 "images", :force => true do |t|
t.string "title"
t.string "resource"
@@ -46,8 +60,11 @@
t.datetime "updated_at", :null => false
t.string "excerpt"
t.integer "category_id"
+ t.string "slug"
end
+ add_index "posts", ["slug"], :name => "index_posts_on_slug"
+
create_table "users", :force => true do |t|
t.string "email", :default => "", :null => false
t.string "encrypted_password", :default => "", :null => false
View
93 db/schema.rb.orig
@@ -0,0 +1,93 @@
+# encoding: UTF-8
+# This file is auto-generated from the current state of the database. Instead
+# of editing this file, please use the migrations feature of Active Record to
+# incrementally modify your database, and then regenerate this schema definition.
+#
+# Note that this schema.rb definition is the authoritative source for your
+# database schema. If you need to create the application database on another
+# system, you should be using db:schema:load, not running all the migrations
+# from scratch. The latter is a flawed and unsustainable approach (the more migrations
+# you'll amass, the slower it'll run and the greater likelihood for issues).
+#
+# It's strongly recommended to check this file into your version control system.
+
+<<<<<<< HEAD
+ActiveRecord::Schema.define(:version => 20120418224035) do
+=======
+ActiveRecord::Schema.define(:version => 20120421113622) do
+>>>>>>> master
+
+ create_table "categories", :force => true do |t|
+ t.string "name"
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ t.string "slug"
+ end
+
+<<<<<<< HEAD
+ create_table "events", :force => true do |t|
+ t.string "title"
+ t.string "url"
+ t.date "start_date"
+ t.date "end_date"
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ t.integer "post_id"
+ end
+
+=======
+ add_index "categories", ["slug"], :name => "index_categories_on_slug"
+
+ 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 => 40
+ t.datetime "created_at"
+ end
+
+ add_index "friendly_id_slugs", ["slug", "sluggable_type"], :name => "index_friendly_id_slugs_on_slug_and_sluggable_type", :unique => true
+ 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"
+
+>>>>>>> master
+ create_table "images", :force => true do |t|
+ t.string "title"
+ t.string "resource"
+ t.integer "post_id"
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ end
+
+ create_table "posts", :force => true do |t|
+ t.string "title"
+ t.text "body"
+ t.datetime "published_at"
+ t.boolean "published"
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ t.string "excerpt"
+ t.integer "category_id"
+ t.string "slug"
+ end
+
+ add_index "posts", ["slug"], :name => "index_posts_on_slug"
+
+ create_table "users", :force => true do |t|
+ t.string "email", :default => "", :null => false
+ t.string "encrypted_password", :default => "", :null => false
+ t.string "reset_password_token"
+ t.datetime "reset_password_sent_at"
+ t.datetime "remember_created_at"
+ t.integer "sign_in_count", :default => 0
+ t.datetime "current_sign_in_at"
+ t.datetime "last_sign_in_at"
+ t.string "current_sign_in_ip"
+ t.string "last_sign_in_ip"
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ end
+
+ add_index "users", ["email"], :name => "index_users_on_email", :unique => true
+ add_index "users", ["reset_password_token"], :name => "index_users_on_reset_password_token", :unique => true
+
+end
View
25 db/seeds.rb
@@ -1,5 +1,6 @@
-# encoding: utf-8
-#
+# -*- encoding: utf-8 -*-
+
+>>>>>>> master
# This file should contain all the record creation needed to seed the database with its default values.
# The data can then be loaded with the rake db:seed (or created alongside the db with db:setup).
#
@@ -17,12 +18,12 @@ def seed_section(title)
def section(message)
$stdout.puts "\n"
- $stdout.puts "===== #{message}"
+ $stdout.puts "== #{message}"
$stdout.puts "\n"
end
def info(message)
- $stdout.puts "----- #{message}"
+ $stdout.puts "-- #{message}"
end
# seeds
@@ -40,8 +41,14 @@ def info(message)
end
end
-
-# TODO: this should be improved as previously seen
-Category.delete_all
-Category.create(name: 'Música')
-Category.create(name: 'Cine')
+seed_section 'Categories' do
+ %w(Música Cine).each do |name|
+ info "Creating the category '#{name}'."
+ if Category.find_by_slug(name.parameterize)
+ info "The category '#{name}' exists. Skipping."
+ else
+ Category.create(name: name)
+ info "Category '#{name}' created."
+ end
+ end
+end
View
66 db/seeds.rb.orig
@@ -0,0 +1,66 @@
+<<<<<<< HEAD
+# encoding: utf-8
+#
+=======
+# -*- encoding: utf-8 -*-
+
+>>>>>>> master
+# This file should contain all the record creation needed to seed the database with its default values.
+# The data can then be loaded with the rake db:seed (or created alongside the db with db:setup).
+#
+# Examples:
+#
+# cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }])
+# Mayor.create(name: 'Emanuel', city: cities.first)
+#
+# helpers
+def seed_section(title)
+ section "BEGIN: #{title}"
+ yield if block_given?
+ section "END"
+end
+
+def section(message)
+ $stdout.puts "\n"
+ $stdout.puts "== #{message}"
+ $stdout.puts "\n"
+end
+
+def info(message)
+ $stdout.puts "-- #{message}"
+end
+
+# seeds
+
+seed_section 'Admin user' do
+ email = 'admin@notedetengas.es'
+ info "Creating the admin user with the email '#{email}'."
+ if User.find_by_email(email)
+ info "The user exists. Skipping."
+ else
+ User.create(email: email,
+ password: 'admin123',
+ password_confirmation: 'admin123')
+ info "User created."
+ end
+end
+
+<<<<<<< HEAD
+
+# TODO: this should be improved as previously seen
+Category.delete_all
+Category.create(name: 'Música')
+Category.create(name: 'Cine')
+=======
+seed_section 'Categories' do
+ %w(Música Cine).each do |name|
+ info "Creating the category '#{name}'."
+ if Category.find_by_slug(name.parameterize)
+ info "The category '#{name}' exists. Skipping."
+ else
+ Category.create(name: name)
+ info "Category '#{name}' created."
+ end
+ end
+end
+>>>>>>> master
View
4 spec/decorators/application_decorator_spec.rb
@@ -1,4 +0,0 @@
-require 'spec_helper'
-
-describe ApplicationDecorator do
-end
View
5 spec/decorators/post_decorator_spec.rb
@@ -1,5 +0,0 @@
-require 'spec_helper'
-
-describe PostDecorator do
- before { ApplicationController.new.set_current_view_context }
-end
View
2 test/factories/categories.rb
@@ -2,6 +2,6 @@
FactoryGirl.define do
factory :category do
- name { Faker::Lorem.words 1 }
+ name { Faker::Lorem.sentence 1 }
end
end
View
1 test/functional/backend/posts_controller_test.rb
@@ -7,6 +7,7 @@ class Backend::PostsControllerTest < ActionController::TestCase
include BackendTestCase
setup { @post = FactoryGirl.create(:post) }
+ teardown { Post.delete_all }
test "should get index" do
get :index
View
25 test/functional/posts_controller_test.rb
@@ -1,7 +1,26 @@
+# -*- encoding: utf-8 -*-
+
require 'test_helper'
class PostsControllerTest < ActionController::TestCase
- # test "the truth" do
- # assert true
- # end
+
+ setup { @post = FactoryGirl.create(:post) }
+ teardown { Post.delete_all }
+
+ test "should show post" do
+ get :show, id: @post
+
+ assert_response :success
+ end
+
+ test "should redirect to new url if the post's slug has changed" do
+ old_slug = @post.slug
+ @post.title = "#{@post.title} #{Faker::Lorem.word}"
+ @post.save
+
+ get :show, id: old_slug
+
+ assert_response :moved_permanently
+ assert_redirected_to @post
+ end
end
View
9 test/unit/category_test.rb
@@ -1,17 +1,20 @@
+# -*- encoding: utf-8 -*-
+
require 'test_helper'
class CategoryTest < ActiveSupport::TestCase
setup { @category = FactoryGirl.create(:category) }
+ teardown { Category.delete_all }
test "name should be unique" do
category_a = FactoryGirl.build(:category, name: "category a")
category_a_bis = FactoryGirl.build(:category, name: "category a")
- category_a.save
+ assert category_a.save
assert category_a.valid?
- category_a_bis.save
+ assert !category_a_bis.save
assert category_a_bis.invalid?
end
@@ -20,5 +23,5 @@ class CategoryTest < ActiveSupport::TestCase
assert @category.invalid?
assert @category.errors[:name].any?
end
-
+
end
View
4 vendor/assets/stylesheets/bootstrap/_variables.scss
@@ -55,8 +55,8 @@ $zindexModalBackdrop: 1040 !default;
$zindexModal: 1050 !default;
// Sprite icons path
-$iconSpritePath: "../img/glyphicons-halflings.png";
-$iconWhiteSpritePath: "../img/glyphicons-halflings-white.png";
+$iconSpritePath: image-path("bootstrap/glyphicons-halflings.png");
+$iconWhiteSpritePath: image-path("bootstrap/glyphicons-halflings-white.png");
// Input placeholder text color
$placeholderText: $grayLight !default;

0 comments on commit 31e2280

Please sign in to comment.
Something went wrong with that request. Please try again.