diff --git a/app/assets/javascripts/comments.js.coffee b/app/assets/javascripts/comments.js.coffee new file mode 100644 index 0000000..7615679 --- /dev/null +++ b/app/assets/javascripts/comments.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/ diff --git a/app/assets/stylesheets/comments.css.scss b/app/assets/stylesheets/comments.css.scss new file mode 100644 index 0000000..3722c12 --- /dev/null +++ b/app/assets/stylesheets/comments.css.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the comments controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb new file mode 100644 index 0000000..78d6a78 --- /dev/null +++ b/app/controllers/comments_controller.rb @@ -0,0 +1,86 @@ +class CommentsController < ApplicationController + # GET /comments + # GET /comments.json + def index + if params[:site_id].nil? or params[:site_id].empty? + @comments = Comment.all + else + @comments = Site.find(params[:site_id]).comments + end + respond_to do |format| + format.html # index.html.erb + format.json { render json: @comments } + end + end + + # GET /comments/1 + # GET /comments/1.json + def show + @comment = Comment.find(params[:id]) + + respond_to do |format| + format.html # show.html.erb + format.json { render json: @comment } + end + end + + # GET /comments/new + # GET /comments/new.json + def new + @comment = Comment.new + + respond_to do |format| + format.html # new.html.erb + format.json { render json: @comment } + end + end + + # GET /comments/1/edit + def edit + @comment = Comment.find(params[:id]) + end + + # POST /comments + # POST /comments.json + def create + @comment = Comment.new(params[:comment]) + + respond_to do |format| + if @comment.save + format.html { redirect_to @comment, notice: 'Comment was successfully created.' } + format.json { render json: @comment, status: :created, location: @comment } + else + format.html { render action: "new" } + format.json { render json: @comment.errors, status: :unprocessable_entity } + end + end + end + + # PUT /comments/1 + # PUT /comments/1.json + def update + @comment = Comment.find(params[:id]) + + respond_to do |format| + if @comment.update_attributes(params[:comment]) + format.html { redirect_to @comment, notice: 'Comment was successfully updated.' } + format.json { head :no_content } + else + format.html { render action: "edit" } + format.json { render json: @comment.errors, status: :unprocessable_entity } + end + end + end + + # DELETE /comments/1 + # DELETE /comments/1.json + def destroy + @comment = Comment.find(params[:id]) + @comment.destroy + + respond_to do |format| + format.html { redirect_to comments_url } + format.json { head :no_content } + end + end +end diff --git a/app/helpers/comments_helper.rb b/app/helpers/comments_helper.rb new file mode 100644 index 0000000..0ec9ca5 --- /dev/null +++ b/app/helpers/comments_helper.rb @@ -0,0 +1,2 @@ +module CommentsHelper +end diff --git a/app/models/comment.rb b/app/models/comment.rb new file mode 100644 index 0000000..d9c8554 --- /dev/null +++ b/app/models/comment.rb @@ -0,0 +1,4 @@ +class Comment < ActiveRecord::Base + belongs_to :user + belongs_to :site +end diff --git a/app/models/site.rb b/app/models/site.rb index 2b2f99a..0e06328 100644 --- a/app/models/site.rb +++ b/app/models/site.rb @@ -4,6 +4,7 @@ class Site < ActiveRecord::Base has_many :visits has_many :trips, :through => :visits has_attached_file :image + has_many :comments # Debe estar protegido para evitar accesos indeseados diff --git a/app/models/user.rb b/app/models/user.rb index 35b8159..d9d8859 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -2,6 +2,7 @@ class User < ActiveRecord::Base has_many :sites has_many :trips + has_many :comments # Include default devise modules. Others available are: # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable diff --git a/app/views/comments/_form.html.erb b/app/views/comments/_form.html.erb new file mode 100644 index 0000000..3036a7c --- /dev/null +++ b/app/views/comments/_form.html.erb @@ -0,0 +1,29 @@ +<%= form_for(@comment) do |f| %> + <% if @comment.errors.any? %> +
+

<%= pluralize(@comment.errors.count, "error") %> prohibited this comment from being saved:

+ + +
+ <% end %> + +
+ <%= f.label :comment %>
+ <%= f.text_field :comment %> +
+
+ <%= f.label :user_id %>
+ <%= f.number_field :user_id %> +
+
+ <%= f.label :site_id %>
+ <%= f.number_field :site_id %> +
+
+ <%= f.submit %> +
+<% end %> diff --git a/app/views/comments/edit.html.erb b/app/views/comments/edit.html.erb new file mode 100644 index 0000000..12ea7f9 --- /dev/null +++ b/app/views/comments/edit.html.erb @@ -0,0 +1,6 @@ +

Editing comment

+ +<%= render 'form' %> + +<%= link_to 'Show', @comment %> | +<%= link_to 'Back', comments_path %> diff --git a/app/views/comments/index.html.erb b/app/views/comments/index.html.erb new file mode 100644 index 0000000..b3c1394 --- /dev/null +++ b/app/views/comments/index.html.erb @@ -0,0 +1,27 @@ +

Listing comments

+ + + + + + + + + + + +<% @comments.each do |comment| %> + + + + + + + + +<% end %> +
CommentUserSite
<%= comment.comment %><%= comment.user_id %><%= comment.site_id %><%= link_to 'Show', comment %><%= link_to 'Edit', edit_comment_path(comment) %><%= link_to 'Destroy', comment, confirm: 'Are you sure?', method: :delete %>
+ +
+ +<%= link_to 'New Comment', new_comment_path %> diff --git a/app/views/comments/new.html.erb b/app/views/comments/new.html.erb new file mode 100644 index 0000000..07a754a --- /dev/null +++ b/app/views/comments/new.html.erb @@ -0,0 +1,5 @@ +

New comment

+ +<%= render 'form' %> + +<%= link_to 'Back', comments_path %> diff --git a/app/views/comments/show.html.erb b/app/views/comments/show.html.erb new file mode 100644 index 0000000..b375949 --- /dev/null +++ b/app/views/comments/show.html.erb @@ -0,0 +1,20 @@ +

<%= notice %>

+ +

+ Comment: + <%= @comment.comment %> +

+ +

+ User: + <%= @comment.user_id %> +

+ +

+ Site: + <%= @comment.site_id %> +

+ + +<%= link_to 'Edit', edit_comment_path(@comment) %> | +<%= link_to 'Back', comments_path %> diff --git a/app/views/sites/index.html.erb b/app/views/sites/index.html.erb index b8e4cae..9bd3177 100644 --- a/app/views/sites/index.html.erb +++ b/app/views/sites/index.html.erb @@ -6,7 +6,7 @@ - <%= link_to image_tag(site.image.url, :class => 'list_image'), site %> + <%= link_to image_tag(site.image_url, :class => 'list_image'), site %> @@ -19,11 +19,13 @@ <%= link_to 'Show', site %>
- <% if site.user == current_user %> <%= link_to 'Edit', edit_site_path(site) %>
+ <% if site.user == current_user %> <%= link_to 'Edit', edit_site_path(site) %>
+ <% if site.comments.length > 0 %> <%= link_to 'Comments', site_comments_path(site) %>
<%= link_to 'Destroy', site, :confirm => 'Are you sure?', :method => :delete %> - <% end %> + <% end %> + <% end %> <% end %> @@ -31,4 +33,4 @@
-<%= link_to 'New site', new_site_path %> \ No newline at end of file +<%= link_to 'New site', new_site_path %> diff --git a/app/views/sites/show.html.erb b/app/views/sites/show.html.erb index 01a5417..3d137be 100644 --- a/app/views/sites/show.html.erb +++ b/app/views/sites/show.html.erb @@ -2,7 +2,7 @@

<%= @site.type.name if @site.type %>

- <%= image_tag(@site.image.url, :class => 'site_image') %> + <%= image_tag(@site.image_url, :class => 'site_image') %>

<%= @site.name %>

@@ -17,3 +17,33 @@

<% if @site.user == current_user %> <%= link_to 'Edit', edit_site_path(@site) %> | <% end %> <%= link_to 'Back', sites_path %> + +

+ + + + + + + + + +<% @site.comments.each do |comment| %> + + + + + + +<% end %> +
CommentUser
<%= comment.comment %><%= comment.user.name if comment.user_id%><%= link_to 'Edit', edit_comment_path(comment) if comment.user == current_user %><%= link_to 'Destroy', comment, confirm: 'Are you sure?', method: :delete if comment.user == current_user %>
+
+
+

Add a comment:

+<%= form_for( @site.comments.build ) do |f| %> + <%= f.label :comment %>
+ <%= f.text_area :comment %> + <%= hidden_field_tag(:user_id, current_user) %> + <%= f.submit %> +<% end %> +
diff --git a/app/views/trips/_trip.html.erb b/app/views/trips/_trip.html.erb index 2d69c99..42e9380 100644 --- a/app/views/trips/_trip.html.erb +++ b/app/views/trips/_trip.html.erb @@ -3,7 +3,7 @@ <% trip.visits.order(:hour).each do |visit| %> - <%= link_to image_tag(visit.site.image.url, :class => 'list_image'), visit.site %> + <%= link_to image_tag(visit.site.image_url, :class => 'list_image'), visit.site %> diff --git a/config/routes.rb b/config/routes.rb index 29d3c86..e8d50ae 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,12 +1,16 @@ Planet::Application.routes.draw do + resources :comments + resources :visits resources :trips devise_for :users - resources :sites + resources :sites do + resources :comments + end resources :types do # Rutas anidadas /types/id/sites..., resources :sites, :only => [ :index ] # Restringe a acción “index” diff --git a/db/migrate/20120415181005_create_comments.rb b/db/migrate/20120415181005_create_comments.rb new file mode 100644 index 0000000..36d0b14 --- /dev/null +++ b/db/migrate/20120415181005_create_comments.rb @@ -0,0 +1,11 @@ +class CreateComments < ActiveRecord::Migration + def change + create_table :comments do |t| + t.string :comment + t.integer :user_id + t.integer :site_id + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index e6aa66f..50f029a 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,15 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20120411160519) do +ActiveRecord::Schema.define(:version => 20120415181005) do + + create_table "comments", :force => true do |t| + t.string "comment" + t.integer "user_id" + t.integer "site_id" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false + end create_table "sites", :force => true do |t| t.string "name" diff --git a/test/fixtures/comments.yml b/test/fixtures/comments.yml new file mode 100644 index 0000000..29c9999 --- /dev/null +++ b/test/fixtures/comments.yml @@ -0,0 +1,11 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html + +one: + comment: MyString + user_id: 1 + site_id: 1 + +two: + comment: MyString + user_id: 1 + site_id: 1 diff --git a/test/functional/comments_controller_test.rb b/test/functional/comments_controller_test.rb new file mode 100644 index 0000000..b2b500c --- /dev/null +++ b/test/functional/comments_controller_test.rb @@ -0,0 +1,49 @@ +require 'test_helper' + +class CommentsControllerTest < ActionController::TestCase + setup do + @comment = comments(:one) + end + + test "should get index" do + get :index + assert_response :success + assert_not_nil assigns(:comments) + end + + test "should get new" do + get :new + assert_response :success + end + + test "should create comment" do + assert_difference('Comment.count') do + post :create, comment: @comment.attributes + end + + assert_redirected_to comment_path(assigns(:comment)) + end + + test "should show comment" do + get :show, id: @comment + assert_response :success + end + + test "should get edit" do + get :edit, id: @comment + assert_response :success + end + + test "should update comment" do + put :update, id: @comment, comment: @comment.attributes + assert_redirected_to comment_path(assigns(:comment)) + end + + test "should destroy comment" do + assert_difference('Comment.count', -1) do + delete :destroy, id: @comment + end + + assert_redirected_to comments_path + end +end diff --git a/test/unit/comment_test.rb b/test/unit/comment_test.rb new file mode 100644 index 0000000..b6d6131 --- /dev/null +++ b/test/unit/comment_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class CommentTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/unit/helpers/comments_helper_test.rb b/test/unit/helpers/comments_helper_test.rb new file mode 100644 index 0000000..2518c16 --- /dev/null +++ b/test/unit/helpers/comments_helper_test.rb @@ -0,0 +1,4 @@ +require 'test_helper' + +class CommentsHelperTest < ActionView::TestCase +end