From 22e8183508858843563999cb376f4f6913863bba Mon Sep 17 00:00:00 2001 From: Nicolas Bally Date: Mon, 20 Aug 2012 18:47:52 +0200 Subject: [PATCH] ajout fonction tiny url --- app/assets/javascripts/tiny_urls.js.coffee | 3 + app/assets/stylesheets/scaffolds.css.scss | 69 +++++++++++++++ app/assets/stylesheets/tiny_urls.css.scss | 3 + app/controllers/tiny_urls_controller.rb | 84 +++++++++++++++++++ app/helpers/tiny_urls_helper.rb | 2 + app/models/tiny_url.rb | 3 + app/views/tiny_urls/_form.html.erb | 22 +++++ app/views/tiny_urls/edit.html.erb | 6 ++ app/views/tiny_urls/index.html.erb | 30 +++++++ app/views/tiny_urls/new.html.erb | 5 ++ app/views/tiny_urls/show.html.erb | 30 +++++++ config/routes.rb | 5 ++ db/migrate/20120820162850_create_tiny_urls.rb | 14 ++++ db/schema.rb | 19 ++++- test/fixtures/tiny_urls.yml | 15 ++++ test/functional/tiny_urls_controller_test.rb | 49 +++++++++++ test/unit/helpers/tiny_urls_helper_test.rb | 4 + test/unit/tiny_url_test.rb | 7 ++ 18 files changed, 366 insertions(+), 4 deletions(-) create mode 100644 app/assets/javascripts/tiny_urls.js.coffee create mode 100644 app/assets/stylesheets/scaffolds.css.scss create mode 100644 app/assets/stylesheets/tiny_urls.css.scss create mode 100644 app/controllers/tiny_urls_controller.rb create mode 100644 app/helpers/tiny_urls_helper.rb create mode 100644 app/models/tiny_url.rb create mode 100644 app/views/tiny_urls/_form.html.erb create mode 100644 app/views/tiny_urls/edit.html.erb create mode 100644 app/views/tiny_urls/index.html.erb create mode 100644 app/views/tiny_urls/new.html.erb create mode 100644 app/views/tiny_urls/show.html.erb create mode 100644 db/migrate/20120820162850_create_tiny_urls.rb create mode 100644 test/fixtures/tiny_urls.yml create mode 100644 test/functional/tiny_urls_controller_test.rb create mode 100644 test/unit/helpers/tiny_urls_helper_test.rb create mode 100644 test/unit/tiny_url_test.rb diff --git a/app/assets/javascripts/tiny_urls.js.coffee b/app/assets/javascripts/tiny_urls.js.coffee new file mode 100644 index 0000000..7615679 --- /dev/null +++ b/app/assets/javascripts/tiny_urls.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/scaffolds.css.scss b/app/assets/stylesheets/scaffolds.css.scss new file mode 100644 index 0000000..6ec6a8f --- /dev/null +++ b/app/assets/stylesheets/scaffolds.css.scss @@ -0,0 +1,69 @@ +body { + background-color: #fff; + color: #333; + font-family: verdana, arial, helvetica, sans-serif; + font-size: 13px; + line-height: 18px; +} + +p, ol, ul, td { + font-family: verdana, arial, helvetica, sans-serif; + font-size: 13px; + line-height: 18px; +} + +pre { + background-color: #eee; + padding: 10px; + font-size: 11px; +} + +a { + color: #000; + &:visited { + color: #666; + } + &:hover { + color: #fff; + background-color: #000; + } +} + +div { + &.field, &.actions { + margin-bottom: 10px; + } +} + +#notice { + color: green; +} + +.field_with_errors { + padding: 2px; + background-color: red; + display: table; +} + +#error_explanation { + width: 450px; + border: 2px solid red; + padding: 7px; + padding-bottom: 0; + margin-bottom: 20px; + background-color: #f0f0f0; + h2 { + text-align: left; + font-weight: bold; + padding: 5px 5px 5px 15px; + font-size: 12px; + margin: -7px; + margin-bottom: 0px; + background-color: #c00; + color: #fff; + } + ul li { + font-size: 12px; + list-style: square; + } +} diff --git a/app/assets/stylesheets/tiny_urls.css.scss b/app/assets/stylesheets/tiny_urls.css.scss new file mode 100644 index 0000000..087a247 --- /dev/null +++ b/app/assets/stylesheets/tiny_urls.css.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the TinyUrls 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/tiny_urls_controller.rb b/app/controllers/tiny_urls_controller.rb new file mode 100644 index 0000000..4614fe3 --- /dev/null +++ b/app/controllers/tiny_urls_controller.rb @@ -0,0 +1,84 @@ +class TinyUrlsController < ApplicationController + + http_basic_authenticate_with :name => "stats", :password => "stats", :except => [:show] + + # GET /tiny_urls + # GET /tiny_urls.json + def index + @tiny_urls = TinyUrl.all + + respond_to do |format| + format.html # index.html.erb + format.json { render json: @tiny_urls } + end + end + + # GET /tiny_urls/1 + # GET /tiny_urls/1.json + def show + @tiny_url = TinyUrl.find(params[:id]) +@tiny_url.nbr_views = @tiny_url.nbr_views.to_i + 1 +@tiny_url.save + redirect_to @tiny_url.url, :status => 301 + end + + # GET /tiny_urls/new + # GET /tiny_urls/new.json + def new + @tiny_url = TinyUrl.new + + respond_to do |format| + format.html # new.html.erb + format.json { render json: @tiny_url } + end + end + + # GET /tiny_urls/1/edit + def edit + @tiny_url = TinyUrl.find(params[:id]) + end + + # POST /tiny_urls + # POST /tiny_urls.json + def create + @tiny_url = TinyUrl.new(params[:tiny_url]) + + respond_to do |format| + if @tiny_url.save + format.html { redirect_to @tiny_url, notice: 'Tiny url was successfully created.' } + format.json { render json: @tiny_url, status: :created, location: @tiny_url } + else + format.html { render action: "new" } + format.json { render json: @tiny_url.errors, status: :unprocessable_entity } + end + end + end + + # PUT /tiny_urls/1 + # PUT /tiny_urls/1.json + def update + @tiny_url = TinyUrl.find(params[:id]) + + respond_to do |format| + if @tiny_url.update_attributes(params[:tiny_url]) + format.html { redirect_to @tiny_url, notice: 'Tiny url was successfully updated.' } + format.json { head :no_content } + else + format.html { render action: "edit" } + format.json { render json: @tiny_url.errors, status: :unprocessable_entity } + end + end + end + + # DELETE /tiny_urls/1 + # DELETE /tiny_urls/1.json + def destroy + @tiny_url = TinyUrl.find(params[:id]) + @tiny_url.destroy + + respond_to do |format| + format.html { redirect_to tiny_urls_url } + format.json { head :no_content } + end + end +end diff --git a/app/helpers/tiny_urls_helper.rb b/app/helpers/tiny_urls_helper.rb new file mode 100644 index 0000000..5078cfa --- /dev/null +++ b/app/helpers/tiny_urls_helper.rb @@ -0,0 +1,2 @@ +module TinyUrlsHelper +end diff --git a/app/models/tiny_url.rb b/app/models/tiny_url.rb new file mode 100644 index 0000000..25e71c0 --- /dev/null +++ b/app/models/tiny_url.rb @@ -0,0 +1,3 @@ +class TinyUrl < ActiveRecord::Base + attr_accessible :nbr_views, :slug, :start_at, :stop_at, :url +end diff --git a/app/views/tiny_urls/_form.html.erb b/app/views/tiny_urls/_form.html.erb new file mode 100644 index 0000000..9b5b1e8 --- /dev/null +++ b/app/views/tiny_urls/_form.html.erb @@ -0,0 +1,22 @@ +<%= form_for(@tiny_url) do |f| %> + <% if @tiny_url.errors.any? %> +
+

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

+ + +
+ <% end %> + +
+ <%= f.label :url %>
+ <%= f.text_field :url %> +
+ +
+ <%= f.submit %> +
+<% end %> diff --git a/app/views/tiny_urls/edit.html.erb b/app/views/tiny_urls/edit.html.erb new file mode 100644 index 0000000..dafcb7a --- /dev/null +++ b/app/views/tiny_urls/edit.html.erb @@ -0,0 +1,6 @@ +

Editing tiny_url

+ +<%= render 'form' %> + +<%= link_to 'Show', @tiny_url %> | +<%= link_to 'Back', tiny_urls_path %> diff --git a/app/views/tiny_urls/index.html.erb b/app/views/tiny_urls/index.html.erb new file mode 100644 index 0000000..1d6e3bd --- /dev/null +++ b/app/views/tiny_urls/index.html.erb @@ -0,0 +1,30 @@ +

Listing tiny_urls

+ + + + + + + + + + + + +<% @tiny_urls.each do |tiny_url| %> + + + + + + + + + + +<% end %> +
Url courteUrlNbr vues
<%= link_to "http://lepicvert.org"+tiny_url_path(tiny_url),"http://lepicvert.org"+tiny_url_path(tiny_url) %><%= tiny_url.url %><%= tiny_url.nbr_views %><%= link_to 'Edit', edit_tiny_url_path(tiny_url) %><%= link_to 'Destroy', tiny_url, method: :delete, data: { confirm: 'Are you sure?' } %>
+ +
+ +<%= link_to 'New Tiny url', new_tiny_url_path %> diff --git a/app/views/tiny_urls/new.html.erb b/app/views/tiny_urls/new.html.erb new file mode 100644 index 0000000..6c53dc3 --- /dev/null +++ b/app/views/tiny_urls/new.html.erb @@ -0,0 +1,5 @@ +

New tiny_url

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

<%= notice %>

+ +

+ Url: + <%= @tiny_url.url %> +

+ +

+ Slug: + <%= @tiny_url.slug %> +

+ +

+ Start at: + <%= @tiny_url.start_at %> +

+ +

+ Stop at: + <%= @tiny_url.stop_at %> +

+ +

+ Nbr views: + <%= @tiny_url.nbr_views %> +

+ + +<%= link_to 'Edit', edit_tiny_url_path(@tiny_url) %> | +<%= link_to 'Back', tiny_urls_path %> diff --git a/config/routes.rb b/config/routes.rb index e592de8..a69b0f6 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,9 @@ Survey::Application.routes.draw do + + match 'u/:id' => 'tiny_urls#show', :as => :tiny_url + + resources :tiny_urls + get "surveys/index" match 'sondages/merci-de-votre-participation.:f' => 'survey_sets#thanks', :as => :thanks_survey_sets, :f => "html" diff --git a/db/migrate/20120820162850_create_tiny_urls.rb b/db/migrate/20120820162850_create_tiny_urls.rb new file mode 100644 index 0000000..144a890 --- /dev/null +++ b/db/migrate/20120820162850_create_tiny_urls.rb @@ -0,0 +1,14 @@ +class CreateTinyUrls < ActiveRecord::Migration + def change + create_table :tiny_urls do |t| + t.string :url + t.string :slug + t.text :description + t.datetime :start_at + t.datetime :stop_at + t.integer :nbr_views + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index e08529d..f3b46d7 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20120816091837) do +ActiveRecord::Schema.define(:version => 20120820162850) do create_table "answer_sets", :force => true do |t| t.integer "question_set_id" @@ -28,10 +28,10 @@ ActiveRecord::Schema.define(:version => 20120816091837) do create_table "question_sets", :force => true do |t| t.integer "survey_item_id" t.integer "survey_set_id" - t.string "content" + t.text "content", :limit => 255 t.boolean "boolean_content" - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false end add_index "question_sets", ["survey_item_id"], :name => "index_question_sets_on_survey_item_id" @@ -91,4 +91,15 @@ ActiveRecord::Schema.define(:version => 20120816091837) do t.datetime "updated_at", :null => false end + create_table "tiny_urls", :force => true do |t| + t.string "url" + t.string "slug" + t.text "description" + t.datetime "start_at" + t.datetime "stop_at" + t.integer "nbr_views" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false + end + end diff --git a/test/fixtures/tiny_urls.yml b/test/fixtures/tiny_urls.yml new file mode 100644 index 0000000..05efe87 --- /dev/null +++ b/test/fixtures/tiny_urls.yml @@ -0,0 +1,15 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html + +one: + url: MyString + slug: MyString + start_at: 2012-08-20 18:28:50 + stop_at: 2012-08-20 18:28:50 + nbr_views: 1 + +two: + url: MyString + slug: MyString + start_at: 2012-08-20 18:28:50 + stop_at: 2012-08-20 18:28:50 + nbr_views: 1 diff --git a/test/functional/tiny_urls_controller_test.rb b/test/functional/tiny_urls_controller_test.rb new file mode 100644 index 0000000..7ca340b --- /dev/null +++ b/test/functional/tiny_urls_controller_test.rb @@ -0,0 +1,49 @@ +require 'test_helper' + +class TinyUrlsControllerTest < ActionController::TestCase + setup do + @tiny_url = tiny_urls(:one) + end + + test "should get index" do + get :index + assert_response :success + assert_not_nil assigns(:tiny_urls) + end + + test "should get new" do + get :new + assert_response :success + end + + test "should create tiny_url" do + assert_difference('TinyUrl.count') do + post :create, tiny_url: { nbr_views: @tiny_url.nbr_views, slug: @tiny_url.slug, start_at: @tiny_url.start_at, stop_at: @tiny_url.stop_at, url: @tiny_url.url } + end + + assert_redirected_to tiny_url_path(assigns(:tiny_url)) + end + + test "should show tiny_url" do + get :show, id: @tiny_url + assert_response :success + end + + test "should get edit" do + get :edit, id: @tiny_url + assert_response :success + end + + test "should update tiny_url" do + put :update, id: @tiny_url, tiny_url: { nbr_views: @tiny_url.nbr_views, slug: @tiny_url.slug, start_at: @tiny_url.start_at, stop_at: @tiny_url.stop_at, url: @tiny_url.url } + assert_redirected_to tiny_url_path(assigns(:tiny_url)) + end + + test "should destroy tiny_url" do + assert_difference('TinyUrl.count', -1) do + delete :destroy, id: @tiny_url + end + + assert_redirected_to tiny_urls_path + end +end diff --git a/test/unit/helpers/tiny_urls_helper_test.rb b/test/unit/helpers/tiny_urls_helper_test.rb new file mode 100644 index 0000000..d378877 --- /dev/null +++ b/test/unit/helpers/tiny_urls_helper_test.rb @@ -0,0 +1,4 @@ +require 'test_helper' + +class TinyUrlsHelperTest < ActionView::TestCase +end diff --git a/test/unit/tiny_url_test.rb b/test/unit/tiny_url_test.rb new file mode 100644 index 0000000..f01c7f1 --- /dev/null +++ b/test/unit/tiny_url_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class TinyUrlTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end