Browse Source

Add Layer model/scaffold with a join table to habtm feeds

master
Adrian Short 11 years ago
parent
commit
206303f9e6
18 changed files with 248 additions and 1 deletions
  1. +3
    -0
      app/assets/javascripts/layers.js.coffee
  2. +3
    -0
      app/assets/stylesheets/layers.css.scss
  3. +83
    -0
      app/controllers/layers_controller.rb
  4. +2
    -0
      app/helpers/layers_helper.rb
  5. +1
    -0
      app/models/feed.rb
  6. +4
    -0
      app/models/layer.rb
  7. +13
    -0
      app/views/layers/_form.html.haml
  8. +7
    -0
      app/views/layers/edit.html.haml
  9. +19
    -0
      app/views/layers/index.html.haml
  10. +5
    -0
      app/views/layers/new.html.haml
  11. +9
    -0
      app/views/layers/show.html.haml
  12. +3
    -0
      config/routes.rb
  13. +15
    -0
      db/migrate/20130228150425_create_layers.rb
  14. +14
    -1
      db/schema.rb
  15. +7
    -0
      test/fixtures/layers.yml
  16. +49
    -0
      test/functional/layers_controller_test.rb
  17. +4
    -0
      test/unit/helpers/layers_helper_test.rb
  18. +7
    -0
      test/unit/layer_test.rb

+ 3
- 0
app/assets/javascripts/layers.js.coffee View File

@@ -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/

+ 3
- 0
app/assets/stylesheets/layers.css.scss View File

@@ -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/

+ 83
- 0
app/controllers/layers_controller.rb View File

@@ -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

+ 2
- 0
app/helpers/layers_helper.rb View File

@@ -0,0 +1,2 @@
module LayersHelper
end

+ 1
- 0
app/models/feed.rb View File

@@ -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"


+ 4
- 0
app/models/layer.rb View File

@@ -0,0 +1,4 @@
class Layer < ActiveRecord::Base
attr_accessible :name
has_and_belongs_to_many :feeds
end

+ 13
- 0
app/views/layers/_form.html.haml View File

@@ -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'

+ 7
- 0
app/views/layers/edit.html.haml View File

@@ -0,0 +1,7 @@
%h1 Editing layer

= render 'form'

= link_to 'Show', @layer
\|
= link_to 'Back', layers_path

+ 19
- 0
app/views/layers/index.html.haml View File

@@ -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

+ 5
- 0
app/views/layers/new.html.haml View File

@@ -0,0 +1,5 @@
%h1 New layer

= render 'form'

= link_to 'Back', layers_path

+ 9
- 0
app/views/layers/show.html.haml View File

@@ -0,0 +1,9 @@
%p#notice= notice

%p
%b Name:
= @layer.name

= link_to 'Edit', edit_layer_path(@layer)
\|
= link_to 'Back', layers_path

+ 3
- 0
config/routes.rb View File

@@ -1,4 +1,7 @@
Apollo::Application.routes.draw do
resources :layers


get "logout" => "sessions#destroy", :as => "logout"
get "login" => "sessions#new", :as => "login"



+ 15
- 0
db/migrate/20130228150425_create_layers.rb View File

@@ -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

+ 14
- 1
db/schema.rb View File

@@ -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"


+ 7
- 0
test/fixtures/layers.yml View File

@@ -0,0 +1,7 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html

one:
name: MyString

two:
name: MyString

+ 49
- 0
test/functional/layers_controller_test.rb View File

@@ -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

+ 4
- 0
test/unit/helpers/layers_helper_test.rb View File

@@ -0,0 +1,4 @@
require 'test_helper'

class LayersHelperTest < ActionView::TestCase
end

+ 7
- 0
test/unit/layer_test.rb View File

@@ -0,0 +1,7 @@
require 'test_helper'

class LayerTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

Loading…
Cancel
Save