Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[Feature] As a user, I should be able to create bookmarks and list them.

  • Loading branch information...
commit 8270cc48535c731432743d97893ffb7af2aacbdc 1 parent fd6c76e
@mongrelion authored
View
3  app/assets/javascripts/bookmarks.js.coffee
@@ -0,0 +1,3 @@
+# Place all the behaviors and hooks related to the matching controller here.
+# All this logic will automatically be available in application.js.
+# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
View
61 app/controllers/bookmarks_controller.rb
@@ -0,0 +1,61 @@
+class BookmarksController < ApplicationController
+
+ before_filter :authenticate_user!
+
+ def index
+ @bookmarks = current_user.bookmarks
+ respond_to do |format|
+ format.html
+ format.json { render :json => @bookmarks }
+ end
+ end
+
+ def show
+ @bookmark = Bookmark.find params[ :id ]
+ respond_to do |format|
+ format.html
+ format.json { render :json => @bookmark }
+ end
+ end
+
+ def new
+ @bookmark = Bookmark.new
+ respond_to do |format|
+ format.html
+ format.json { render :json => @bookmark }
+ end
+ end
+
+ def create
+ @bookmark = Bookmark.create params[ :bookmark ]
+ respond_to do |format|
+ format.html do
+ if @bookmark.persisted?
+ flash[ :notice ] = 'Bookmark successfully created.'
+ redirect_to bookmark_path( @bookmark )
+ else
+ flash[ :error ] = 'Bookmark could not be saved.'
+ render :new
+ end
+ end
+ format.json do
+ result = {}
+ if @bookmark.persisted?
+ result = {
+ :status => 'ok',
+ :message => 'Bookmark successfully created',
+ :bookmark => @bookmark
+ }
+ else
+ result = {
+ :status => 'error',
+ :message => 'Bookmark could not be saved.',
+ :errors => @bookmark.errors.full_messages
+ }
+ end
+ render :json => result
+ end
+ end
+ end
+
+end
View
2  app/helpers/bookmarks_helper.rb
@@ -0,0 +1,2 @@
+module BookmarksHelper
+end
View
10 app/models/bookmark.rb
@@ -4,6 +4,9 @@ class Bookmark
# - Relationships -
has_and_belongs_to_many :users
+ # - Attribute Accessors -
+ attr_accessor :dirty_tags
+
# - Fields -
field :url, type: String
field :title, type: String
@@ -18,6 +21,7 @@ class Bookmark
validates_presence_of :url
# - Callbacks -
+ before_save :clean_dirty_tags
before_save :sanitize_tags
# - Instance Methods -
@@ -33,4 +37,10 @@ def sanitize_tags
self.tags = self.tags.map( &:downcase ).uniq
end
+ def clean_dirty_tags
+ if self.dirty_tags.is_a? String
+ self.tags = self.dirty_tags.split( ',' ).map { |tag| tag.gsub( ' ', '' ) }
+ end
+ end
+
end
View
16 app/views/bookmarks/_form.html.slim
@@ -0,0 +1,16 @@
+= form_for @bookmark do |f|
+ = error_messages_for @bookmark
+ fieldset
+ = f.label :url
+ = f.text_field :url
+ fieldset
+ = f.label :title
+ = f.text_field :title
+ fieldset
+ = f.label :tags
+ = f.text_field :dirty_tags
+ br/
+ small
+ i Separate tags by commas: ruby, rails, ror, php, mongodb, nosql
+ fieldset
+ = f.submit
View
11 app/views/bookmarks/index.html.slim
@@ -0,0 +1,11 @@
+h1 My bookmarks
+- unless @bookmarks.empty?
+ - @bookmarks.each do |bookmark|
+ .bookmark
+ span.title = link_to bookmark.title || bookmark.url, bookmark_path( bookmark )
+ span.users-count = bookmark.users_count
+ - unless bookmark.tags.empty?
+ ul
+ - bookmark.tags.each do |tag|
+ li = tag
+= link_to 'Back', root_path
View
3  app/views/bookmarks/new.html.slim
@@ -0,0 +1,3 @@
+h1 New bookmark
+= render 'form'
+= link_to 'Back', root_path
View
13 app/views/bookmarks/show.html.slim
@@ -0,0 +1,13 @@
+' Title:
+= @bookmark.title
+br/
+' URL:
+= link_to @bookmark.url, @bookmark.url
+br/
+' Tags:
+= @bookmark.tags.join ', '
+br/
+' Users count:
+= @bookmark.users_count
+br/
+= link_to 'Back', bookmarks_path
View
5 config/routes.rb
@@ -4,5 +4,10 @@
# - Devise Section -
devise_for :users
+ # - End Devise Section -
+
+ # - Bookmarks Section -
+ resources :bookmarks
+ # - End Bookmarks Section -
end
View
69 spec/controllers/bookmarks_controller_spec.rb
@@ -0,0 +1,69 @@
+require 'spec_helper'
+
+describe BookmarksController do
+
+ before :all do
+ @user = Fabricate( :user )
+ @bookmarks = [
+ Fabricate( :bookmark, :users => [ @user ] ),
+ Fabricate( :bookmark, :users => [ @user ] ),
+ Fabricate( :bookmark )
+ ]
+ end
+
+ describe "GET 'index'" do
+ it "should be successful" do
+ sign_in @user
+ get 'index'
+ response.should be_success
+ end
+ it "assigns the user's bookmarks to @bookmarks" do
+ sign_in @user
+ get 'index'
+ assigns( :bookmarks ).should eq( @user.bookmarks )
+ end
+ end
+
+ describe "GET 'show'" do
+ it "should be successful" do
+ sign_in @user
+ get 'show', :id => @bookmarks.first.id
+ response.should be_success
+ end
+ it "assigns a bookmark to @bookmark" do
+ sign_in @user
+ get 'show', :id => @bookmarks.first.id
+ assigns( :bookmark ).should eq( @bookmarks.first )
+ end
+ end
+
+ describe "GET 'new'" do
+ it "should be successful" do
+ sign_in @user
+ get 'new'
+ response.should be_success
+ end
+ end
+
+ describe "POST 'create'" do
+ it "should be successful" do
+ sign_in @user
+ post 'create'
+ response.should be_success
+ end
+ it "should create a bookmark if it doesn't exist in database" do
+ sign_in @user
+ expect {
+ bookmark = Fabricate( :bookmark, :users => [ @user ] )
+ post 'create', Hash.from_xml( bookmark.to_xml )
+ }.to change( @user, :bookmarks_count ).by( 1 )
+ end
+ it "should not create a bookmark if it already exists in database" do
+ sign_in @user
+ expect {
+ post 'create', Hash.from_xml( @bookmarks.first.to_xml )
+ }.to change( Bookmark, :count ).by( 0 )
+ end
+ end
+
+end
View
14 spec/controllers/home_controller_spec.rb
@@ -4,17 +4,15 @@
describe "GET 'index'" do
- before :each do
- get :index
+ it "should be successful" do
+ get 'index'
+ response.should be_success
end
it "assigns all bookmarks to @bookmarks" do
- bookmark = Fabricate( :bookmark )
- assigns( :bookmarks ).should eq( [ bookmark ] )
- end
-
- it "should be successful" do
- response.should be_success
+ bookmarks = Bookmark.all
+ get 'index'
+ assigns( :bookmarks ).should eq( bookmarks )
end
end
View
2  spec/fabricators/bookmark_fabricator.rb
@@ -1,5 +1,5 @@
Fabricator( :bookmark ) do
- url 'http://mongodb.org'
+ url "http://mongodb#{ Fabricate.sequence :number }.org"
title 'MongoDB'
tags %w[ nosql sql mongodb document db database ]
end
View
8 spec/models/bookmark_spec.rb
@@ -33,4 +33,12 @@
bookmark.tags.should eq( nifty_tags )
end
+ it "should receive a comma separated list of tags and normalize it" do
+ dirty_tags = 'mongo db, no sql, sql'
+ nifty_tags = %w[ mongodb nosql sql ]
+ bookmark = Fabricate.build( :bookmark, :dirty_tags => dirty_tags )
+ bookmark.save
+ bookmark.tags.should eq( nifty_tags )
+ end
+
end
View
5 spec/views/bookmarks/edit.html.slim_spec.rb
@@ -0,0 +1,5 @@
+require 'spec_helper'
+
+describe "bookmarks/edit.html.slim" do
+ pending "add some examples to (or delete) #{__FILE__}"
+end
View
5 spec/views/bookmarks/index.html.slim_spec.rb
@@ -0,0 +1,5 @@
+require 'spec_helper'
+
+describe "bookmarks/index.html.slim" do
+ pending "add some examples to (or delete) #{__FILE__}"
+end
View
5 spec/views/bookmarks/new.html.slim_spec.rb
@@ -0,0 +1,5 @@
+require 'spec_helper'
+
+describe "bookmarks/new.html.slim" do
+ pending "add some examples to (or delete) #{__FILE__}"
+end
View
5 spec/views/bookmarks/show.html.slim_spec.rb
@@ -0,0 +1,5 @@
+require 'spec_helper'
+
+describe "bookmarks/show.html.slim" do
+ pending "add some examples to (or delete) #{__FILE__}"
+end
Please sign in to comment.
Something went wrong with that request. Please try again.