From 206303f9e685e0d1527f1738a9cacbce434222e9 Mon Sep 17 00:00:00 2001 From: Adrian Short Date: Thu, 28 Feb 2013 15:14:06 +0000 Subject: [PATCH] Add Layer model/scaffold with a join table to habtm feeds --- app/assets/javascripts/layers.js.coffee | 3 + app/assets/stylesheets/layers.css.scss | 3 + app/controllers/layers_controller.rb | 83 ++++++++++++++++++++++ app/helpers/layers_helper.rb | 2 + app/models/feed.rb | 1 + app/models/layer.rb | 4 ++ app/views/layers/_form.html.haml | 13 ++++ app/views/layers/edit.html.haml | 7 ++ app/views/layers/index.html.haml | 19 +++++ app/views/layers/new.html.haml | 5 ++ app/views/layers/show.html.haml | 9 +++ config/routes.rb | 3 + db/migrate/20130228150425_create_layers.rb | 15 ++++ db/schema.rb | 15 +++- test/fixtures/layers.yml | 7 ++ test/functional/layers_controller_test.rb | 49 +++++++++++++ test/unit/helpers/layers_helper_test.rb | 4 ++ test/unit/layer_test.rb | 7 ++ 18 files changed, 248 insertions(+), 1 deletion(-) create mode 100644 app/assets/javascripts/layers.js.coffee create mode 100644 app/assets/stylesheets/layers.css.scss create mode 100644 app/controllers/layers_controller.rb create mode 100644 app/helpers/layers_helper.rb create mode 100644 app/models/layer.rb create mode 100644 app/views/layers/_form.html.haml create mode 100644 app/views/layers/edit.html.haml create mode 100644 app/views/layers/index.html.haml create mode 100644 app/views/layers/new.html.haml create mode 100644 app/views/layers/show.html.haml create mode 100644 db/migrate/20130228150425_create_layers.rb create mode 100644 test/fixtures/layers.yml create mode 100644 test/functional/layers_controller_test.rb create mode 100644 test/unit/helpers/layers_helper_test.rb create mode 100644 test/unit/layer_test.rb diff --git a/app/assets/javascripts/layers.js.coffee b/app/assets/javascripts/layers.js.coffee new file mode 100644 index 0000000..7615679 --- /dev/null +++ b/app/assets/javascripts/layers.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/layers.css.scss b/app/assets/stylesheets/layers.css.scss new file mode 100644 index 0000000..3a0518b --- /dev/null +++ b/app/assets/stylesheets/layers.css.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the Layers 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/layers_controller.rb b/app/controllers/layers_controller.rb new file mode 100644 index 0000000..430a7ae --- /dev/null +++ b/app/controllers/layers_controller.rb @@ -0,0 +1,83 @@ +class LayersController < ApplicationController + # GET /layers + # GET /layers.json + def index + @layers = Layer.all + + respond_to do |format| + format.html # index.html.erb + format.json { render json: @layers } + end + end + + # GET /layers/1 + # GET /layers/1.json + def show + @layer = Layer.find(params[:id]) + + respond_to do |format| + format.html # show.html.erb + format.json { render json: @layer } + end + end + + # GET /layers/new + # GET /layers/new.json + def new + @layer = Layer.new + + respond_to do |format| + format.html # new.html.erb + format.json { render json: @layer } + end + end + + # GET /layers/1/edit + def edit + @layer = Layer.find(params[:id]) + end + + # POST /layers + # POST /layers.json + def create + @layer = Layer.new(params[:layer]) + + respond_to do |format| + if @layer.save + format.html { redirect_to @layer, notice: 'Layer was successfully created.' } + format.json { render json: @layer, status: :created, location: @layer } + else + format.html { render action: "new" } + format.json { render json: @layer.errors, status: :unprocessable_entity } + end + end + end + + # PUT /layers/1 + # PUT /layers/1.json + def update + @layer = Layer.find(params[:id]) + + respond_to do |format| + if @layer.update_attributes(params[:layer]) + format.html { redirect_to @layer, notice: 'Layer was successfully updated.' } + format.json { head :no_content } + else + format.html { render action: "edit" } + format.json { render json: @layer.errors, status: :unprocessable_entity } + end + end + end + + # DELETE /layers/1 + # DELETE /layers/1.json + def destroy + @layer = Layer.find(params[:id]) + @layer.destroy + + respond_to do |format| + format.html { redirect_to layers_url } + format.json { head :no_content } + end + end +end diff --git a/app/helpers/layers_helper.rb b/app/helpers/layers_helper.rb new file mode 100644 index 0000000..b8dbc16 --- /dev/null +++ b/app/helpers/layers_helper.rb @@ -0,0 +1,2 @@ +module LayersHelper +end diff --git a/app/models/feed.rb b/app/models/feed.rb index b826fc0..755bb04 100644 --- a/app/models/feed.rb +++ b/app/models/feed.rb @@ -1,5 +1,6 @@ class Feed < ActiveRecord::Base has_many :posts, :dependent => :destroy + has_and_belongs_to_many :layers attr_accessible :title, :url, :description, :generator, :last_fetched, :feed_url validates_format_of :feed_url, :with => URI::regexp(%w(http https)), :message => "must be a valid URL" diff --git a/app/models/layer.rb b/app/models/layer.rb new file mode 100644 index 0000000..86d2ff0 --- /dev/null +++ b/app/models/layer.rb @@ -0,0 +1,4 @@ +class Layer < ActiveRecord::Base + attr_accessible :name + has_and_belongs_to_many :feeds +end diff --git a/app/views/layers/_form.html.haml b/app/views/layers/_form.html.haml new file mode 100644 index 0000000..27bc898 --- /dev/null +++ b/app/views/layers/_form.html.haml @@ -0,0 +1,13 @@ += form_for @layer do |f| + - if @layer.errors.any? + #error_explanation + %h2= "#{pluralize(@layer.errors.count, "error")} prohibited this layer from being saved:" + %ul + - @layer.errors.full_messages.each do |msg| + %li= msg + + .field + = f.label :name + = f.text_field :name + .actions + = f.submit 'Save' diff --git a/app/views/layers/edit.html.haml b/app/views/layers/edit.html.haml new file mode 100644 index 0000000..9c013b7 --- /dev/null +++ b/app/views/layers/edit.html.haml @@ -0,0 +1,7 @@ +%h1 Editing layer + += render 'form' + += link_to 'Show', @layer +\| += link_to 'Back', layers_path diff --git a/app/views/layers/index.html.haml b/app/views/layers/index.html.haml new file mode 100644 index 0000000..29618df --- /dev/null +++ b/app/views/layers/index.html.haml @@ -0,0 +1,19 @@ +%h1 Listing layers + +%table + %tr + %th Name + %th + %th + %th + + - @layers.each do |layer| + %tr + %td= layer.name + %td= link_to 'Show', layer + %td= link_to 'Edit', edit_layer_path(layer) + %td= link_to 'Destroy', layer, :method => :delete, :data => { :confirm => 'Are you sure?' } + +%br + += link_to 'New Layer', new_layer_path diff --git a/app/views/layers/new.html.haml b/app/views/layers/new.html.haml new file mode 100644 index 0000000..73ddc96 --- /dev/null +++ b/app/views/layers/new.html.haml @@ -0,0 +1,5 @@ +%h1 New layer + += render 'form' + += link_to 'Back', layers_path diff --git a/app/views/layers/show.html.haml b/app/views/layers/show.html.haml new file mode 100644 index 0000000..3bad3dc --- /dev/null +++ b/app/views/layers/show.html.haml @@ -0,0 +1,9 @@ +%p#notice= notice + +%p + %b Name: + = @layer.name + += link_to 'Edit', edit_layer_path(@layer) +\| += link_to 'Back', layers_path diff --git a/config/routes.rb b/config/routes.rb index 6cbea69..288e017 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,7 @@ Apollo::Application.routes.draw do + resources :layers + + get "logout" => "sessions#destroy", :as => "logout" get "login" => "sessions#new", :as => "login" diff --git a/db/migrate/20130228150425_create_layers.rb b/db/migrate/20130228150425_create_layers.rb new file mode 100644 index 0000000..4ad96d7 --- /dev/null +++ b/db/migrate/20130228150425_create_layers.rb @@ -0,0 +1,15 @@ +class CreateLayers < ActiveRecord::Migration + def change + create_table :layers do |t| + t.string :name + t.timestamps + end + + create_table :feeds_layers, :id => false do |t| + t.integer :feed_id + t.integer :layer_id + end + + add_index :feeds_layers, [ :feed_id, :layer_id ] + end +end diff --git a/db/schema.rb b/db/schema.rb index 1b9c52f..f6c1318 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 => 20130222125833) do +ActiveRecord::Schema.define(:version => 20130228150425) do create_table "feeds", :force => true do |t| t.string "title" @@ -24,6 +24,19 @@ ActiveRecord::Schema.define(:version => 20130222125833) do t.datetime "last_fetched" end + create_table "feeds_layers", :id => false, :force => true do |t| + t.integer "feed_id" + t.integer "layer_id" + end + + add_index "feeds_layers", ["feed_id", "layer_id"], :name => "index_feeds_layers_on_feed_id_and_layer_id" + + create_table "layers", :force => true do |t| + t.string "name" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false + end + create_table "posts", :force => true do |t| t.string "title" t.string "url" diff --git a/test/fixtures/layers.yml b/test/fixtures/layers.yml new file mode 100644 index 0000000..0227c60 --- /dev/null +++ b/test/fixtures/layers.yml @@ -0,0 +1,7 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html + +one: + name: MyString + +two: + name: MyString diff --git a/test/functional/layers_controller_test.rb b/test/functional/layers_controller_test.rb new file mode 100644 index 0000000..d6dc765 --- /dev/null +++ b/test/functional/layers_controller_test.rb @@ -0,0 +1,49 @@ +require 'test_helper' + +class LayersControllerTest < ActionController::TestCase + setup do + @layer = layers(:one) + end + + test "should get index" do + get :index + assert_response :success + assert_not_nil assigns(:layers) + end + + test "should get new" do + get :new + assert_response :success + end + + test "should create layer" do + assert_difference('Layer.count') do + post :create, layer: { name: @layer.name } + end + + assert_redirected_to layer_path(assigns(:layer)) + end + + test "should show layer" do + get :show, id: @layer + assert_response :success + end + + test "should get edit" do + get :edit, id: @layer + assert_response :success + end + + test "should update layer" do + put :update, id: @layer, layer: { name: @layer.name } + assert_redirected_to layer_path(assigns(:layer)) + end + + test "should destroy layer" do + assert_difference('Layer.count', -1) do + delete :destroy, id: @layer + end + + assert_redirected_to layers_path + end +end diff --git a/test/unit/helpers/layers_helper_test.rb b/test/unit/helpers/layers_helper_test.rb new file mode 100644 index 0000000..ad81667 --- /dev/null +++ b/test/unit/helpers/layers_helper_test.rb @@ -0,0 +1,4 @@ +require 'test_helper' + +class LayersHelperTest < ActionView::TestCase +end diff --git a/test/unit/layer_test.rb b/test/unit/layer_test.rb new file mode 100644 index 0000000..29c3aa3 --- /dev/null +++ b/test/unit/layer_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class LayerTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end