From 9a741fff8376e16537a07936d8eb0f825d7c98e1 Mon Sep 17 00:00:00 2001 From: Marcos Augusto Date: Fri, 8 May 2009 21:30:12 -0300 Subject: [PATCH] Attachment conflict !! --- app/controllers/attachments_controller.rb | 85 --------- app/controllers/medias_controller.rb | 85 +++++++++ ...attachments_helper.rb => medias_helper.rb} | 2 +- app/models/article.rb | 2 +- app/models/holder.rb | 20 ++ app/models/{attachment.rb => media.rb} | 12 +- app/views/articles/_article.html.haml | 4 +- app/views/attachments/index.html.haml | 20 -- app/views/attachments/show.html.erb | 13 -- app/views/layouts/application.html.erb | 2 +- .../_attachment.html.haml | 5 +- app/views/medias/_nested.html.haml | 5 + .../{attachments => medias}/edit.html.haml | 2 +- app/views/medias/index.html.haml | 20 ++ .../{attachments => medias}/new.html.haml | 2 +- app/views/medias/show.html.erb | 13 ++ config/database.yml.sample | 46 +++++ .../20090319012124_create_attachments.rb | 16 +- lib/autocomplete.rb | 20 ++ .../attachments_controller_spec.rb | 173 ------------------ spec/controllers/medias_controller_spec.rb | 173 ++++++++++++++++++ spec/helpers/attachments_helper_spec.rb | 8 +- .../{attachment_spec.rb => media_spec.rb} | 4 +- spec/routing/attachments_routing_spec.rb | 59 ------ spec/routing/medias_routing_spec.rb | 59 ++++++ spec/views/attachments/edit.html.erb_spec.rb | 22 --- spec/views/attachments/new.html.erb_spec.rb | 22 --- spec/views/medias/edit.html.erb_spec.rb | 22 +++ .../index.html.erb_spec.rb | 14 +- spec/views/medias/new.html.erb_spec.rb | 22 +++ .../show.html.erb_spec.rb | 8 +- 31 files changed, 525 insertions(+), 435 deletions(-) delete mode 100644 app/controllers/attachments_controller.rb create mode 100644 app/controllers/medias_controller.rb rename app/helpers/{attachments_helper.rb => medias_helper.rb} (89%) rename app/models/{attachment.rb => media.rb} (83%) delete mode 100644 app/views/attachments/index.html.haml delete mode 100644 app/views/attachments/show.html.erb rename app/views/{attachments => medias}/_attachment.html.haml (50%) create mode 100644 app/views/medias/_nested.html.haml rename app/views/{attachments => medias}/edit.html.haml (67%) create mode 100644 app/views/medias/index.html.haml rename app/views/{attachments => medias}/new.html.haml (61%) create mode 100644 app/views/medias/show.html.erb create mode 100644 config/database.yml.sample delete mode 100644 spec/controllers/attachments_controller_spec.rb create mode 100644 spec/controllers/medias_controller_spec.rb rename spec/models/{attachment_spec.rb => media_spec.rb} (91%) delete mode 100644 spec/routing/attachments_routing_spec.rb create mode 100644 spec/routing/medias_routing_spec.rb delete mode 100644 spec/views/attachments/edit.html.erb_spec.rb delete mode 100644 spec/views/attachments/new.html.erb_spec.rb create mode 100644 spec/views/medias/edit.html.erb_spec.rb rename spec/views/{attachments => medias}/index.html.erb_spec.rb (52%) create mode 100644 spec/views/medias/new.html.erb_spec.rb rename spec/views/{attachments => medias}/show.html.erb_spec.rb (58%) diff --git a/app/controllers/attachments_controller.rb b/app/controllers/attachments_controller.rb deleted file mode 100644 index 83a033e..0000000 --- a/app/controllers/attachments_controller.rb +++ /dev/null @@ -1,85 +0,0 @@ -class AttachmentsController < ApplicationController - # GET /attachments - # GET /attachments.xml - def index - @attachments = Attachment.search(params[:filter],params[:page]) - - respond_to do |format| - format.html # index.html.erb - format.xml { render :xml => @attachments } - end - end - - # GET /attachments/1 - # GET /attachments/1.xml - def show - @attachment = Attachment.find(params[:id]) - - respond_to do |format| - format.html # show.html.erb - format.xml { render :xml => @attachment } - end - end - - # GET /attachments/new - # GET /attachments/new.xml - def new - @attachment = Attachment.new - - respond_to do |format| - format.html # new.html.erb - format.xml { render :xml => @attachment } - end - end - - # GET /attachments/1/edit - def edit - @attachment = Attachment.find(params[:id]) - end - - # POST /attachments - # POST /attachments.xml - def create - @attachment = Attachment.new(params[:attachment]) - - respond_to do |format| - if @attachment.save - flash[:notice] = 'Attachment was successfully created.' - format.html { redirect_to(@attachment) } - format.xml { render :xml => @attachment, :status => :created, :location => @attachment } - else - format.html { render :action => "new" } - format.xml { render :xml => @attachment.errors, :status => :unprocessable_entity } - end - end - end - - # PUT /attachments/1 - # PUT /attachments/1.xml - def update - @attachment = Attachment.find(params[:id]) - - respond_to do |format| - if @attachment.update_attributes(params[:attachment]) - flash[:notice] = 'Attachment was successfully updated.' - format.html { redirect_to(@attachment) } - format.xml { head :ok } - else - format.html { render :action => "edit" } - format.xml { render :xml => @attachment.errors, :status => :unprocessable_entity } - end - end - end - - # DELETE /attachments/1 - # DELETE /attachments/1.xml - def destroy - @attachment = Attachment.find(params[:id]) - @attachment.destroy - - respond_to do |format| - format.html { redirect_to(attachments_url) } - format.xml { head :ok } - end - end -end diff --git a/app/controllers/medias_controller.rb b/app/controllers/medias_controller.rb new file mode 100644 index 0000000..fd6df8d --- /dev/null +++ b/app/controllers/medias_controller.rb @@ -0,0 +1,85 @@ +class MediasController < ApplicationController + # GET /medias + # GET /medias.xml + def index + @medias = Media.search(params[:filter],params[:page]) + + respond_to do |format| + format.html # index.html.erb + format.xml { render :xml => @medias } + end + end + + # GET /medias/1 + # GET /medias/1.xml + def show + @media = Media.find(params[:id]) + + respond_to do |format| + format.html # show.html.erb + format.xml { render :xml => @media } + end + end + + # GET /medias/new + # GET /medias/new.xml + def new + @media = Media.new + + respond_to do |format| + format.html # new.html.erb + format.xml { render :xml => @media } + end + end + + # GET /medias/1/edit + def edit + @media = Media.find(params[:id]) + end + + # POST /medias + # POST /medias.xml + def create + @media = Media.new(params[:media]) + + respond_to do |format| + if @media.save + flash[:notice] = 'Media was successfully created.' + format.html { redirect_to(@media) } + format.xml { render :xml => @media, :status => :created, :location => @media } + else + format.html { render :action => "new" } + format.xml { render :xml => @media.errors, :status => :unprocessable_entity } + end + end + end + + # PUT /medias/1 + # PUT /medias/1.xml + def update + @media = Media.find(params[:id]) + + respond_to do |format| + if @media.update_attributes(params[:media]) + flash[:notice] = 'Media was successfully updated.' + format.html { redirect_to(@media) } + format.xml { head :ok } + else + format.html { render :action => "edit" } + format.xml { render :xml => @media.errors, :status => :unprocessable_entity } + end + end + end + + # DELETE /medias/1 + # DELETE /medias/1.xml + def destroy + @media = Media.find(params[:id]) + @media.destroy + + respond_to do |format| + format.html { redirect_to(medias_url) } + format.xml { head :ok } + end + end +end diff --git a/app/helpers/attachments_helper.rb b/app/helpers/medias_helper.rb similarity index 89% rename from app/helpers/attachments_helper.rb rename to app/helpers/medias_helper.rb index e1538b1..fe2e1bf 100644 --- a/app/helpers/attachments_helper.rb +++ b/app/helpers/medias_helper.rb @@ -1,4 +1,4 @@ -module AttachmentsHelper +module MediasHelper def choose_icon(mime_type) case mime_type when /\.doc|odf|txt/ then "xls.png" diff --git a/app/models/article.rb b/app/models/article.rb index 4203df0..6d9496c 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -20,7 +20,7 @@ # class Article < ActiveRecord::Base belongs_to :user - has_many :attachments, :as => :attachable + has_many :medias, :as => :attachable has_friendly_id :title, :use_slug => true validates_presence_of :user diff --git a/app/models/holder.rb b/app/models/holder.rb index 4fd469f..ef9ccb5 100644 --- a/app/models/holder.rb +++ b/app/models/holder.rb @@ -1,3 +1,23 @@ +# -*- coding: utf-8 -*- +# +# Copyright © 2009 Fireho +# +# This file is part of SIGA. +# +# SIGA is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# SIGA is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# +# class Holder < ActiveRecord::Base attr_writer :person_text belongs_to :group diff --git a/app/models/attachment.rb b/app/models/media.rb similarity index 83% rename from app/models/attachment.rb rename to app/models/media.rb index 3380087..df7ba7a 100644 --- a/app/models/attachment.rb +++ b/app/models/media.rb @@ -18,14 +18,14 @@ # along with this program. If not, see . # # -class Attachment < ActiveRecord::Base +class Media < ActiveRecord::Base belongs_to :attachable, :polymorphic => true has_attached_file :media, :styles => { :thumb => ["200x200#", :png] }, - :path => ':rails_root/public/files/attachments/:id/:style/:basename.:extension', - :url => '/files/attachments/:id/:style/:basename.:extension' - validates_attachment_presence :media - + :path => ':rails_root/public/files/medias/:id/:style/:basename.:extension', + :url => '/files/medias/:id/:style/:basename.:extension' + validates_media_presence :media + def self.search(filter, page) paginate :per_page => 20, :page => page, :conditions => ['info like ?', "%#{filter}%"], @@ -37,7 +37,7 @@ def self.search(filter, page) # == Schema Information # Schema version: 99999999999999 # -# Table name: attachments +# Table name: medias # # id :integer not null, primary key # attachable_id :integer diff --git a/app/views/articles/_article.html.haml b/app/views/articles/_article.html.haml index 8806526..57a12ff 100644 --- a/app/views/articles/_article.html.haml +++ b/app/views/articles/_article.html.haml @@ -8,8 +8,8 @@ = f.input :public = f.input :published - f.inputs :name => "Arquivo", :id => "arquivos" do - = f.render_associated_form @article.attachments, :partial => "attachments/attachment" - = f.add_associated_link "+ Novo arquivo", :attachments, :partial => "attachments/attachment" + = f.render_associated_form @article.medias, :partial => "medias/nested" + = f.add_associated_link "+ Novo arquivo", :medias, :partial => "medias/nested" =# f.input :published_at, :as => :string, :input_html => { :id => "calendar" } .group.navform = f.submit 'Salvar', :class => :button diff --git a/app/views/attachments/index.html.haml b/app/views/attachments/index.html.haml deleted file mode 100644 index 46d123e..0000000 --- a/app/views/attachments/index.html.haml +++ /dev/null @@ -1,20 +0,0 @@ -.block - .head - %h2.title Attachable - .content - %table{:class => "table"} - %tbody - %tr - %th Tipo - %th Info - -@attachments.each do |attachment| - %tr - -if attachment.media_content_type =~ /^image/ - %td= image_tag attachment.media.url(:thumb) - -else - %td== #{image_tag("/images/mime/#{choose_icon attachment.media_file_name}")} #{attachment.media_file_name} - %td= attachment.info - - = will_paginate @attachments -- sidebar do - = link_to 'Novo Registro', new_attachment_path, :class => 'icon iadd' \ No newline at end of file diff --git a/app/views/attachments/show.html.erb b/app/views/attachments/show.html.erb deleted file mode 100644 index 689cd7e..0000000 --- a/app/views/attachments/show.html.erb +++ /dev/null @@ -1,13 +0,0 @@ -

- Attachable: - <%=h @attachment.attachable %> -

- -

- Info: - <%=h @attachment.info %> -

- - -<%= link_to 'Edit', edit_attachment_path(@attachment) %> | -<%= link_to 'Back', attachments_path %> \ No newline at end of file diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 4b083e8..fc44a70 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -152,7 +152,7 @@
Sistema Gestao Integrada
-
SIG@ Alpha / 2009 - Powered by Ruby, Rails & PostgreSQL
+

- Prefeitura Municial de Xlandia

diff --git a/app/views/attachments/_attachment.html.haml b/app/views/medias/_attachment.html.haml similarity index 50% rename from app/views/attachments/_attachment.html.haml rename to app/views/medias/_attachment.html.haml index 818c5a5..8bfd690 100644 --- a/app/views/attachments/_attachment.html.haml +++ b/app/views/medias/_attachment.html.haml @@ -1,9 +1,8 @@ -- semantic_form_for(@attachment, :html => {:multipart => true, :class => "form"}) do |f| +- semantic_form_for(@media, :html => {:multipart => true, :class => "form"}) do |f| = f.input :media, :label => "Arquivo" - = f.input :info .clear .navform = f.submit "Salvar →", :class => "button" ou - = link_to 'Voltar', attachments_path + = link_to 'Voltar', medias_path diff --git a/app/views/medias/_nested.html.haml b/app/views/medias/_nested.html.haml new file mode 100644 index 0000000..25d2c30 --- /dev/null +++ b/app/views/medias/_nested.html.haml @@ -0,0 +1,5 @@ +.media + - f.inputs do + = f.input :kind, :as => :select, :collection => Media.get_kind_values + = f.input :value, :input_html => { :size => 15 } + = f.remove_link "remove" diff --git a/app/views/attachments/edit.html.haml b/app/views/medias/edit.html.haml similarity index 67% rename from app/views/attachments/edit.html.haml rename to app/views/medias/edit.html.haml index 3ca4e8b..478a38e 100644 --- a/app/views/attachments/edit.html.haml +++ b/app/views/medias/edit.html.haml @@ -1,4 +1,4 @@ .block .content %h2{:class => "title"} Editando Registro - .inner= render @attachment \ No newline at end of file + .inner= render @media \ No newline at end of file diff --git a/app/views/medias/index.html.haml b/app/views/medias/index.html.haml new file mode 100644 index 0000000..3ee5e9d --- /dev/null +++ b/app/views/medias/index.html.haml @@ -0,0 +1,20 @@ +.block + .head + %h2.title Attachable + .content + %table{:class => "table"} + %tbody + %tr + %th Tipo + %th Info + -@medias.each do |media| + %tr + -if media.media_content_type =~ /^image/ + %td= image_tag media.media.url(:thumb) + -else + %td== #{image_tag("/images/mime/#{choose_icon media.media_file_name}")} #{media.media_file_name} + %td= media.info + + = will_paginate @medias +- sidebar do + = link_to 'Novo Registro', new_media_path, :class => 'icon iadd' \ No newline at end of file diff --git a/app/views/attachments/new.html.haml b/app/views/medias/new.html.haml similarity index 61% rename from app/views/attachments/new.html.haml rename to app/views/medias/new.html.haml index 8fa76a1..471b2d5 100644 --- a/app/views/attachments/new.html.haml +++ b/app/views/medias/new.html.haml @@ -2,4 +2,4 @@ .head %h2 Novo registro .content - .inner= render @attachment \ No newline at end of file + .inner= render @media \ No newline at end of file diff --git a/app/views/medias/show.html.erb b/app/views/medias/show.html.erb new file mode 100644 index 0000000..b7b4e22 --- /dev/null +++ b/app/views/medias/show.html.erb @@ -0,0 +1,13 @@ +

+ Attachable: + <%=h @media.attachable %> +

+ +

+ Info: + <%=h @media.info %> +

+ + +<%= link_to 'Edit', edit_media_path(@media) %> | +<%= link_to 'Back', medias_path %> diff --git a/config/database.yml.sample b/config/database.yml.sample new file mode 100644 index 0000000..38e4179 --- /dev/null +++ b/config/database.yml.sample @@ -0,0 +1,46 @@ +# SIGA +# +# PostgreSQL. 8.x are supported. +# +development: + adapter: postgresql + encoding: unicode + database: siga_development + pool: 5 + username: postgres + template: template_postgis + #host: localhost + #port: 5432 + +# Warning: The database defined as "test" will be erased and +# re-generated from your development database when you run "rake". +# Do not set this db to the same as development or production. +test: + adapter: postgresql + encoding: unicode + database: siga_test + template: template_postgis + pool: 5 + username: postgres + +production: + adapter: postgresql + encoding: unicode + database: siga_production + pool: 5 + username: postgres + + # Connect on a TCP socket. Omitted by default since the client uses a + # domain socket that doesn't need configuration. Windows does not have + # domain sockets, so uncomment these lines. + #host: localhost + #port: 5432 + + # Schema search path. The server defaults to $user,public + #schema_search_path: myapp,sharedapp,public + + # Minimum log levels, in increasing order: + # debug5, debug4, debug3, debug2, debug1, + # log, notice, warning, error, fatal, and panic + # The server defaults to notice. + #min_messages: warning diff --git a/db/migrate/20090319012124_create_attachments.rb b/db/migrate/20090319012124_create_attachments.rb index dbb6afd..7fbb37c 100644 --- a/db/migrate/20090319012124_create_attachments.rb +++ b/db/migrate/20090319012124_create_attachments.rb @@ -1,6 +1,6 @@ -class CreateAttachments < ActiveRecord::Migration +class CreateMedias < ActiveRecord::Migration def self.up - create_table :attachments do |t| + create_table :medias do |t| t.references :attachable, :polymorphic => true t.string :media_file_name, :media_content_type t.integer :media_file_size @@ -11,14 +11,14 @@ def self.up t.timestamps end - add_index :attachments, :media_content_type - add_index :attachments, :media_file_name - add_index :attachments, :media_file_size - add_index :attachments, [:attachable_id, :attachable_type] - add_index :attachments, :created_at + add_index :medias, :media_content_type + add_index :medias, :media_file_name + add_index :medias, :media_file_size + add_index :medias, [:attachable_id, :attachable_type] + add_index :medias, :created_at end def self.down - drop_table :attachments + drop_table :medias end end diff --git a/lib/autocomplete.rb b/lib/autocomplete.rb index 50182e5..b79f5d1 100644 --- a/lib/autocomplete.rb +++ b/lib/autocomplete.rb @@ -1,3 +1,23 @@ +# -*- coding: utf-8 -*- +# +# Copyright © 2009 Fireho +# +# This file is part of SIGA. +# +# SIGA is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# SIGA is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# +# module Autocomplete def self.included(base) diff --git a/spec/controllers/attachments_controller_spec.rb b/spec/controllers/attachments_controller_spec.rb deleted file mode 100644 index 9b3826c..0000000 --- a/spec/controllers/attachments_controller_spec.rb +++ /dev/null @@ -1,173 +0,0 @@ -require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') - -describe AttachmentsController do - - def mock_attachment(stubs={}) - @mock_attachment ||= mock_model(Attachment, stubs) - end - - describe "responding to GET index" do - - it "should expose all attachments as @attachments" do - Attachment.should_receive(:find).with(:all).and_return([mock_attachment]) - get :index - assigns[:attachments].should == [mock_attachment] - end - - describe "with mime type of xml" do - - it "should render all attachments as xml" do - request.env["HTTP_ACCEPT"] = "application/xml" - Attachment.should_receive(:find).with(:all).and_return(attachments = mock("Array of Attachments")) - attachments.should_receive(:to_xml).and_return("generated XML") - get :index - response.body.should == "generated XML" - end - - end - - end - - describe "responding to GET show" do - - it "should expose the requested attachment as @attachment" do - Attachment.should_receive(:find).with("37").and_return(mock_attachment) - get :show, :id => "37" - assigns[:attachment].should equal(mock_attachment) - end - - describe "with mime type of xml" do - - it "should render the requested attachment as xml" do - request.env["HTTP_ACCEPT"] = "application/xml" - Attachment.should_receive(:find).with("37").and_return(mock_attachment) - mock_attachment.should_receive(:to_xml).and_return("generated XML") - get :show, :id => "37" - response.body.should == "generated XML" - end - - end - - end - - describe "responding to GET new" do - - it "should expose a new attachment as @attachment" do - Attachment.should_receive(:new).and_return(mock_attachment) - get :new - assigns[:attachment].should equal(mock_attachment) - end - - end - - describe "responding to GET edit" do - - it "should expose the requested attachment as @attachment" do - Attachment.should_receive(:find).with("37").and_return(mock_attachment) - get :edit, :id => "37" - assigns[:attachment].should equal(mock_attachment) - end - - end - - describe "responding to POST create" do - - describe "with valid params" do - - it "should expose a newly created attachment as @attachment" do - Attachment.should_receive(:new).with({'these' => 'params'}).and_return(mock_attachment(:save => true)) - post :create, :attachment => {:these => 'params'} - assigns(:attachment).should equal(mock_attachment) - end - - it "should redirect to the created attachment" do - Attachment.stub!(:new).and_return(mock_attachment(:save => true)) - post :create, :attachment => {} - response.should redirect_to(attachment_url(mock_attachment)) - end - - end - - describe "with invalid params" do - - it "should expose a newly created but unsaved attachment as @attachment" do - Attachment.stub!(:new).with({'these' => 'params'}).and_return(mock_attachment(:save => false)) - post :create, :attachment => {:these => 'params'} - assigns(:attachment).should equal(mock_attachment) - end - - it "should re-render the 'new' template" do - Attachment.stub!(:new).and_return(mock_attachment(:save => false)) - post :create, :attachment => {} - response.should render_template('new') - end - - end - - end - - describe "responding to PUT udpate" do - - describe "with valid params" do - - it "should update the requested attachment" do - Attachment.should_receive(:find).with("37").and_return(mock_attachment) - mock_attachment.should_receive(:update_attributes).with({'these' => 'params'}) - put :update, :id => "37", :attachment => {:these => 'params'} - end - - it "should expose the requested attachment as @attachment" do - Attachment.stub!(:find).and_return(mock_attachment(:update_attributes => true)) - put :update, :id => "1" - assigns(:attachment).should equal(mock_attachment) - end - - it "should redirect to the attachment" do - Attachment.stub!(:find).and_return(mock_attachment(:update_attributes => true)) - put :update, :id => "1" - response.should redirect_to(attachment_url(mock_attachment)) - end - - end - - describe "with invalid params" do - - it "should update the requested attachment" do - Attachment.should_receive(:find).with("37").and_return(mock_attachment) - mock_attachment.should_receive(:update_attributes).with({'these' => 'params'}) - put :update, :id => "37", :attachment => {:these => 'params'} - end - - it "should expose the attachment as @attachment" do - Attachment.stub!(:find).and_return(mock_attachment(:update_attributes => false)) - put :update, :id => "1" - assigns(:attachment).should equal(mock_attachment) - end - - it "should re-render the 'edit' template" do - Attachment.stub!(:find).and_return(mock_attachment(:update_attributes => false)) - put :update, :id => "1" - response.should render_template('edit') - end - - end - - end - - describe "responding to DELETE destroy" do - - it "should destroy the requested attachment" do - Attachment.should_receive(:find).with("37").and_return(mock_attachment) - mock_attachment.should_receive(:destroy) - delete :destroy, :id => "37" - end - - it "should redirect to the attachments list" do - Attachment.stub!(:find).and_return(mock_attachment(:destroy => true)) - delete :destroy, :id => "1" - response.should redirect_to(attachments_url) - end - - end - -end diff --git a/spec/controllers/medias_controller_spec.rb b/spec/controllers/medias_controller_spec.rb new file mode 100644 index 0000000..482c9f2 --- /dev/null +++ b/spec/controllers/medias_controller_spec.rb @@ -0,0 +1,173 @@ +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') + +describe MediasController do + + def mock_media(stubs={}) + @mock_media ||= mock_model(Media, stubs) + end + + describe "responding to GET index" do + + it "should expose all medias as @medias" do + Media.should_receive(:find).with(:all).and_return([mock_media]) + get :index + assigns[:medias].should == [mock_media] + end + + describe "with mime type of xml" do + + it "should render all medias as xml" do + request.env["HTTP_ACCEPT"] = "application/xml" + Media.should_receive(:find).with(:all).and_return(medias = mock("Array of Medias")) + medias.should_receive(:to_xml).and_return("generated XML") + get :index + response.body.should == "generated XML" + end + + end + + end + + describe "responding to GET show" do + + it "should expose the requested media as @media" do + Media.should_receive(:find).with("37").and_return(mock_media) + get :show, :id => "37" + assigns[:media].should equal(mock_media) + end + + describe "with mime type of xml" do + + it "should render the requested media as xml" do + request.env["HTTP_ACCEPT"] = "application/xml" + Media.should_receive(:find).with("37").and_return(mock_media) + mock_media.should_receive(:to_xml).and_return("generated XML") + get :show, :id => "37" + response.body.should == "generated XML" + end + + end + + end + + describe "responding to GET new" do + + it "should expose a new media as @media" do + Media.should_receive(:new).and_return(mock_media) + get :new + assigns[:media].should equal(mock_media) + end + + end + + describe "responding to GET edit" do + + it "should expose the requested media as @media" do + Media.should_receive(:find).with("37").and_return(mock_media) + get :edit, :id => "37" + assigns[:media].should equal(mock_media) + end + + end + + describe "responding to POST create" do + + describe "with valid params" do + + it "should expose a newly created media as @media" do + Media.should_receive(:new).with({'these' => 'params'}).and_return(mock_media(:save => true)) + post :create, :media => {:these => 'params'} + assigns(:media).should equal(mock_media) + end + + it "should redirect to the created media" do + Media.stub!(:new).and_return(mock_media(:save => true)) + post :create, :media => {} + response.should redirect_to(media_url(mock_media)) + end + + end + + describe "with invalid params" do + + it "should expose a newly created but unsaved media as @media" do + Media.stub!(:new).with({'these' => 'params'}).and_return(mock_media(:save => false)) + post :create, :media => {:these => 'params'} + assigns(:media).should equal(mock_media) + end + + it "should re-render the 'new' template" do + Media.stub!(:new).and_return(mock_media(:save => false)) + post :create, :media => {} + response.should render_template('new') + end + + end + + end + + describe "responding to PUT udpate" do + + describe "with valid params" do + + it "should update the requested media" do + Media.should_receive(:find).with("37").and_return(mock_media) + mock_media.should_receive(:update_attributes).with({'these' => 'params'}) + put :update, :id => "37", :media => {:these => 'params'} + end + + it "should expose the requested media as @media" do + Media.stub!(:find).and_return(mock_media(:update_attributes => true)) + put :update, :id => "1" + assigns(:media).should equal(mock_media) + end + + it "should redirect to the media" do + Media.stub!(:find).and_return(mock_media(:update_attributes => true)) + put :update, :id => "1" + response.should redirect_to(media_url(mock_media)) + end + + end + + describe "with invalid params" do + + it "should update the requested media" do + Media.should_receive(:find).with("37").and_return(mock_media) + mock_media.should_receive(:update_attributes).with({'these' => 'params'}) + put :update, :id => "37", :media => {:these => 'params'} + end + + it "should expose the media as @media" do + Media.stub!(:find).and_return(mock_media(:update_attributes => false)) + put :update, :id => "1" + assigns(:media).should equal(mock_media) + end + + it "should re-render the 'edit' template" do + Media.stub!(:find).and_return(mock_media(:update_attributes => false)) + put :update, :id => "1" + response.should render_template('edit') + end + + end + + end + + describe "responding to DELETE destroy" do + + it "should destroy the requested media" do + Media.should_receive(:find).with("37").and_return(mock_media) + mock_media.should_receive(:destroy) + delete :destroy, :id => "37" + end + + it "should redirect to the medias list" do + Media.stub!(:find).and_return(mock_media(:destroy => true)) + delete :destroy, :id => "1" + response.should redirect_to(medias_url) + end + + end + +end diff --git a/spec/helpers/attachments_helper_spec.rb b/spec/helpers/attachments_helper_spec.rb index bece902..da05ab0 100644 --- a/spec/helpers/attachments_helper_spec.rb +++ b/spec/helpers/attachments_helper_spec.rb @@ -1,11 +1,11 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') -describe AttachmentsHelper do - +describe MediasHelper do + #Delete this example and add some real ones or delete this file it "should be included in the object returned by #helper" do included_modules = (class << helper; self; end).send :included_modules - included_modules.should include(AttachmentsHelper) + included_modules.should include(MediasHelper) end - + end diff --git a/spec/models/attachment_spec.rb b/spec/models/media_spec.rb similarity index 91% rename from spec/models/attachment_spec.rb rename to spec/models/media_spec.rb index 59b859f..f94376b 100644 --- a/spec/models/attachment_spec.rb +++ b/spec/models/media_spec.rb @@ -1,6 +1,6 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') -describe Attachment do +describe Media do end @@ -8,7 +8,7 @@ # == Schema Information # Schema version: 99999999999999 # -# Table name: attachments +# Table name: medias # # id :integer not null, primary key # attachable_id :integer diff --git a/spec/routing/attachments_routing_spec.rb b/spec/routing/attachments_routing_spec.rb deleted file mode 100644 index b543f15..0000000 --- a/spec/routing/attachments_routing_spec.rb +++ /dev/null @@ -1,59 +0,0 @@ -require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') - -describe AttachmentsController do - describe "route generation" do - it "should map #index" do - route_for(:controller => "attachments", :action => "index").should == "/attachments" - end - - it "should map #new" do - route_for(:controller => "attachments", :action => "new").should == "/attachments/new" - end - - it "should map #show" do - route_for(:controller => "attachments", :action => "show", :id => "1").should == "/attachments/1" - end - - it "should map #edit" do - route_for(:controller => "attachments", :action => "edit", :id => "1").should == "/attachments/1/edit" - end - - it "should map #update" do - route_for(:controller => "attachments", :action => "update", :id => "1").should == {:path => "/attachments/1", :method => :put } - end - - it "should map #destroy" do - route_for(:controller => "attachments", :action => "destroy", :id => "1").should == {:path => "/attachments/1", :method => :delete } - end - end - - describe "route recognition" do - it "should generate params for #index" do - params_from(:get, "/attachments").should == {:controller => "attachments", :action => "index"} - end - - it "should generate params for #new" do - params_from(:get, "/attachments/new").should == {:controller => "attachments", :action => "new"} - end - - it "should generate params for #create" do - params_from(:post, "/attachments").should == {:controller => "attachments", :action => "create"} - end - - it "should generate params for #show" do - params_from(:get, "/attachments/1").should == {:controller => "attachments", :action => "show", :id => "1"} - end - - it "should generate params for #edit" do - params_from(:get, "/attachments/1/edit").should == {:controller => "attachments", :action => "edit", :id => "1"} - end - - it "should generate params for #update" do - params_from(:put, "/attachments/1").should == {:controller => "attachments", :action => "update", :id => "1"} - end - - it "should generate params for #destroy" do - params_from(:delete, "/attachments/1").should == {:controller => "attachments", :action => "destroy", :id => "1"} - end - end -end diff --git a/spec/routing/medias_routing_spec.rb b/spec/routing/medias_routing_spec.rb new file mode 100644 index 0000000..c8f5e43 --- /dev/null +++ b/spec/routing/medias_routing_spec.rb @@ -0,0 +1,59 @@ +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') + +describe MediasController do + describe "route generation" do + it "should map #index" do + route_for(:controller => "medias", :action => "index").should == "/medias" + end + + it "should map #new" do + route_for(:controller => "medias", :action => "new").should == "/medias/new" + end + + it "should map #show" do + route_for(:controller => "medias", :action => "show", :id => "1").should == "/medias/1" + end + + it "should map #edit" do + route_for(:controller => "medias", :action => "edit", :id => "1").should == "/medias/1/edit" + end + + it "should map #update" do + route_for(:controller => "medias", :action => "update", :id => "1").should == {:path => "/medias/1", :method => :put } + end + + it "should map #destroy" do + route_for(:controller => "medias", :action => "destroy", :id => "1").should == {:path => "/medias/1", :method => :delete } + end + end + + describe "route recognition" do + it "should generate params for #index" do + params_from(:get, "/medias").should == {:controller => "medias", :action => "index"} + end + + it "should generate params for #new" do + params_from(:get, "/medias/new").should == {:controller => "medias", :action => "new"} + end + + it "should generate params for #create" do + params_from(:post, "/medias").should == {:controller => "medias", :action => "create"} + end + + it "should generate params for #show" do + params_from(:get, "/medias/1").should == {:controller => "medias", :action => "show", :id => "1"} + end + + it "should generate params for #edit" do + params_from(:get, "/medias/1/edit").should == {:controller => "medias", :action => "edit", :id => "1"} + end + + it "should generate params for #update" do + params_from(:put, "/medias/1").should == {:controller => "medias", :action => "update", :id => "1"} + end + + it "should generate params for #destroy" do + params_from(:delete, "/medias/1").should == {:controller => "medias", :action => "destroy", :id => "1"} + end + end +end diff --git a/spec/views/attachments/edit.html.erb_spec.rb b/spec/views/attachments/edit.html.erb_spec.rb deleted file mode 100644 index ad073c0..0000000 --- a/spec/views/attachments/edit.html.erb_spec.rb +++ /dev/null @@ -1,22 +0,0 @@ -require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') - -describe "/attachments/edit.html.erb" do - include AttachmentsHelper - - before(:each) do - assigns[:attachment] = @attachment = stub_model(Attachment, - :new_record? => false, - :info => "value for info" - ) - end - - it "should render edit form" do - render "/attachments/edit.html.erb" - - response.should have_tag("form[action=#{attachment_path(@attachment)}][method=post]") do - with_tag('textarea#attachment_info[name=?]', "attachment[info]") - end - end -end - - diff --git a/spec/views/attachments/new.html.erb_spec.rb b/spec/views/attachments/new.html.erb_spec.rb deleted file mode 100644 index 1939d75..0000000 --- a/spec/views/attachments/new.html.erb_spec.rb +++ /dev/null @@ -1,22 +0,0 @@ -require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') - -describe "/attachments/new.html.erb" do - include AttachmentsHelper - - before(:each) do - assigns[:attachment] = stub_model(Attachment, - :new_record? => true, - :info => "value for info" - ) - end - - it "should render new form" do - render "/attachments/new.html.erb" - - response.should have_tag("form[action=?][method=post]", attachments_path) do - with_tag("textarea#attachment_info[name=?]", "attachment[info]") - end - end -end - - diff --git a/spec/views/medias/edit.html.erb_spec.rb b/spec/views/medias/edit.html.erb_spec.rb new file mode 100644 index 0000000..d94f5b2 --- /dev/null +++ b/spec/views/medias/edit.html.erb_spec.rb @@ -0,0 +1,22 @@ +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') + +describe "/medias/edit.html.erb" do + include MediasHelper + + before(:each) do + assigns[:media] = @media = stub_model(Media, + :new_record? => false, + :info => "value for info" + ) + end + + it "should render edit form" do + render "/medias/edit.html.erb" + + response.should have_tag("form[action=#{media_path(@media)}][method=post]") do + with_tag('textarea#media_info[name=?]', "media[info]") + end + end +end + + diff --git a/spec/views/attachments/index.html.erb_spec.rb b/spec/views/medias/index.html.erb_spec.rb similarity index 52% rename from spec/views/attachments/index.html.erb_spec.rb rename to spec/views/medias/index.html.erb_spec.rb index 1fdd428..7e598f0 100644 --- a/spec/views/attachments/index.html.erb_spec.rb +++ b/spec/views/medias/index.html.erb_spec.rb @@ -1,21 +1,21 @@ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') -describe "/attachments/index.html.erb" do - include AttachmentsHelper +describe "/medias/index.html.erb" do + include MediasHelper before(:each) do - assigns[:attachments] = [ - stub_model(Attachment, + assigns[:medias] = [ + stub_model(Media, :info => "value for info" ), - stub_model(Attachment, + stub_model(Media, :info => "value for info" ) ] end - it "should render list of attachments" do - render "/attachments/index.html.erb" + it "should render list of medias" do + render "/medias/index.html.erb" response.should have_tag("tr>td", "value for info".to_s, 2) end end diff --git a/spec/views/medias/new.html.erb_spec.rb b/spec/views/medias/new.html.erb_spec.rb new file mode 100644 index 0000000..4ff4f4a --- /dev/null +++ b/spec/views/medias/new.html.erb_spec.rb @@ -0,0 +1,22 @@ +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') + +describe "/medias/new.html.erb" do + include MediasHelper + + before(:each) do + assigns[:media] = stub_model(Media, + :new_record? => true, + :info => "value for info" + ) + end + + it "should render new form" do + render "/medias/new.html.erb" + + response.should have_tag("form[action=?][method=post]", medias_path) do + with_tag("textarea#media_info[name=?]", "media[info]") + end + end +end + + diff --git a/spec/views/attachments/show.html.erb_spec.rb b/spec/views/medias/show.html.erb_spec.rb similarity index 58% rename from spec/views/attachments/show.html.erb_spec.rb rename to spec/views/medias/show.html.erb_spec.rb index 6d52156..0ff4d83 100644 --- a/spec/views/attachments/show.html.erb_spec.rb +++ b/spec/views/medias/show.html.erb_spec.rb @@ -1,15 +1,15 @@ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') -describe "/attachments/show.html.erb" do - include AttachmentsHelper +describe "/medias/show.html.erb" do + include MediasHelper before(:each) do - assigns[:attachment] = @attachment = stub_model(Attachment, + assigns[:media] = @media = stub_model(Media, :info => "value for info" ) end it "should render attributes in

" do - render "/attachments/show.html.erb" + render "/medias/show.html.erb" response.should have_text(/value\ for\ info/) end end