#8 Add explore menu scaffold to CRUD explore menus

This commit is contained in:
2015-09-13 21:34:03 +08:00
parent d3ae566e79
commit c469a3a136
27 changed files with 570 additions and 5 deletions
@@ -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://coffeescript.org/
@@ -0,0 +1,3 @@
// Place all the styles related to the explore_menus controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
+73
View File
@@ -0,0 +1,73 @@
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;
}
}
+2 -4
View File
@@ -1,10 +1,8 @@
class Api::V1::ExploreController < Api::V1::ApplicationController
def index
first_menu = {key:'top_in_week', url: 'http://baidu.com'}
second_menu = {key:'top_in_month', url: 'http://qq.com'}
menus = [first_menu, second_menu]
render json: menus.to_json
render json: ExploreMenu.order(created_at: :desc).all
end
end
@@ -0,0 +1,9 @@
class ExploreMenusController < InheritedResources::Base
private
def explore_menu_params
params.require(:explore_menu).permit(:name, :resource_url, :icon_url, :description)
end
end
+2
View File
@@ -0,0 +1,2 @@
module ExploreMenusHelper
end
+2
View File
@@ -0,0 +1,2 @@
class ExploreMenu < ActiveRecord::Base
end
@@ -0,0 +1,3 @@
class ExploreMenuSerializer < ActiveModel::Serializer
attributes :id, :name, :resource_url, :icon_url, :description
end
+33
View File
@@ -0,0 +1,33 @@
<%= form_for(@explore_menu) do |f| %>
<% if @explore_menu.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@explore_menu.errors.count, "error") %> prohibited this explore_menu from being saved:</h2>
<ul>
<% @explore_menu.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :resource_url %><br>
<%= f.text_field :resource_url %>
</div>
<div class="field">
<%= f.label :icon_url %><br>
<%= f.text_field :icon_url %>
</div>
<div class="field">
<%= f.label :description %><br>
<%= f.text_field :description %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
+6
View File
@@ -0,0 +1,6 @@
<h1>Editing Explore Menu</h1>
<%= render 'form' %>
<%= link_to 'Show', @explore_menu %> |
<%= link_to 'Back', explore_menus_path %>
+33
View File
@@ -0,0 +1,33 @@
<p id="notice"><%= notice %></p>
<h1>Listing Explore Menus</h1>
<table>
<thead>
<tr>
<th>Name</th>
<th>Resource url</th>
<th>Icon url</th>
<th>Description</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @explore_menus.each do |explore_menu| %>
<tr>
<td><%= explore_menu.name %></td>
<td><%= explore_menu.resource_url %></td>
<td><%= explore_menu.icon_url %></td>
<td><%= explore_menu.description %></td>
<td><%= link_to 'Show', explore_menu %></td>
<td><%= link_to 'Edit', edit_explore_menu_path(explore_menu) %></td>
<td><%= link_to 'Destroy', explore_menu, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Explore menu', new_explore_menu_path %>
@@ -0,0 +1,4 @@
json.array!(@explore_menus) do |explore_menu|
json.extract! explore_menu, :id, :name, :resource_url, :icon_url, :description
json.url explore_menu_url(explore_menu, format: :json)
end
+5
View File
@@ -0,0 +1,5 @@
<h1>New Explore Menu</h1>
<%= render 'form' %>
<%= link_to 'Back', explore_menus_path %>
+24
View File
@@ -0,0 +1,24 @@
<p id="notice"><%= notice %></p>
<p>
<strong>Name:</strong>
<%= @explore_menu.name %>
</p>
<p>
<strong>Resource url:</strong>
<%= @explore_menu.resource_url %>
</p>
<p>
<strong>Icon url:</strong>
<%= @explore_menu.icon_url %>
</p>
<p>
<strong>Description:</strong>
<%= @explore_menu.description %>
</p>
<%= link_to 'Edit', edit_explore_menu_path(@explore_menu) %> |
<%= link_to 'Back', explore_menus_path %>
@@ -0,0 +1 @@
json.extract! @explore_menu, :id, :name, :resource_url, :icon_url, :description, :created_at, :updated_at
+6
View File
@@ -60,6 +60,12 @@ Rails.application.routes.draw do
# # (app/controllers/admin/products_controller.rb)
# resources :products
# end
# namespace :admin do
# resources :explore_menus, class:'ExploreMenu'
# end
resources :explore_menus, :path => "admin/explore_menus"
namespace :api do
namespace :v1 do
@@ -0,0 +1,12 @@
class CreateExploreMenus < ActiveRecord::Migration
def change
create_table :explore_menus do |t|
t.string :name
t.string :resource_url
t.string :icon_url
t.string :description
t.timestamps null: false
end
end
end
+19 -1
View File
@@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20150812140609) do
ActiveRecord::Schema.define(version: 20150913132033) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@@ -31,6 +31,15 @@ ActiveRecord::Schema.define(version: 20150812140609) do
add_index "active_admin_comments", ["namespace"], name: "index_active_admin_comments_on_namespace", using: :btree
add_index "active_admin_comments", ["resource_type", "resource_id"], name: "index_active_admin_comments_on_resource_type_and_resource_id", using: :btree
create_table "admin_explore_menus", force: :cascade do |t|
t.string "name"
t.string "resource_url"
t.string "icon_url"
t.string "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "admin_users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
@@ -67,6 +76,15 @@ ActiveRecord::Schema.define(version: 20150812140609) do
t.integer "likes_count", default: 0
end
create_table "explore_menus", force: :cascade do |t|
t.string "name"
t.string "resource_url"
t.string "icon_url"
t.string "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "favorites", force: :cascade do |t|
t.integer "user_id"
t.integer "favoritable_id"
@@ -0,0 +1,159 @@
require 'rails_helper'
# This spec was generated by rspec-rails when you ran the scaffold generator.
# It demonstrates how one might use RSpec to specify the controller code that
# was generated by Rails when you ran the scaffold generator.
#
# It assumes that the implementation code is generated by the rails scaffold
# generator. If you are using any extension libraries to generate different
# controller code, this generated spec may or may not pass.
#
# It only uses APIs available in rails and/or rspec-rails. There are a number
# of tools you can use to make these specs even more expressive, but we're
# sticking to rails and rspec-rails APIs to keep things simple and stable.
#
# Compared to earlier versions of this generator, there is very limited use of
# stubs and message expectations in this spec. Stubs are only used when there
# is no simpler way to get a handle on the object needed for the example.
# Message expectations are only used when there is no simpler way to specify
# that an instance is receiving a specific message.
RSpec.describe ExploreMenusController, type: :controller do
# This should return the minimal set of attributes required to create a valid
# ExploreMenu. As you add validations to ExploreMenu, be sure to
# adjust the attributes here as well.
let(:valid_attributes) {
skip("Add a hash of attributes valid for your model")
}
let(:invalid_attributes) {
skip("Add a hash of attributes invalid for your model")
}
# This should return the minimal set of values that should be in the session
# in order to pass any filters (e.g. authentication) defined in
# ExploreMenusController. Be sure to keep this updated too.
let(:valid_session) { {} }
describe "GET #index" do
it "assigns all explore_menus as @explore_menus" do
explore_menu = ExploreMenu.create! valid_attributes
get :index, {}, valid_session
expect(assigns(:explore_menus)).to eq([explore_menu])
end
end
describe "GET #show" do
it "assigns the requested explore_menu as @explore_menu" do
explore_menu = ExploreMenu.create! valid_attributes
get :show, {:id => explore_menu.to_param}, valid_session
expect(assigns(:explore_menu)).to eq(explore_menu)
end
end
describe "GET #new" do
it "assigns a new explore_menu as @explore_menu" do
get :new, {}, valid_session
expect(assigns(:explore_menu)).to be_a_new(ExploreMenu)
end
end
describe "GET #edit" do
it "assigns the requested explore_menu as @explore_menu" do
explore_menu = ExploreMenu.create! valid_attributes
get :edit, {:id => explore_menu.to_param}, valid_session
expect(assigns(:explore_menu)).to eq(explore_menu)
end
end
describe "POST #create" do
context "with valid params" do
it "creates a new ExploreMenu" do
expect {
post :create, {:explore_menu => valid_attributes}, valid_session
}.to change(ExploreMenu, :count).by(1)
end
it "assigns a newly created explore_menu as @explore_menu" do
post :create, {:explore_menu => valid_attributes}, valid_session
expect(assigns(:explore_menu)).to be_a(ExploreMenu)
expect(assigns(:explore_menu)).to be_persisted
end
it "redirects to the created explore_menu" do
post :create, {:explore_menu => valid_attributes}, valid_session
expect(response).to redirect_to(ExploreMenu.last)
end
end
context "with invalid params" do
it "assigns a newly created but unsaved explore_menu as @explore_menu" do
post :create, {:explore_menu => invalid_attributes}, valid_session
expect(assigns(:explore_menu)).to be_a_new(ExploreMenu)
end
it "re-renders the 'new' template" do
post :create, {:explore_menu => invalid_attributes}, valid_session
expect(response).to render_template("new")
end
end
end
describe "PUT #update" do
context "with valid params" do
let(:new_attributes) {
skip("Add a hash of attributes valid for your model")
}
it "updates the requested explore_menu" do
explore_menu = ExploreMenu.create! valid_attributes
put :update, {:id => explore_menu.to_param, :explore_menu => new_attributes}, valid_session
explore_menu.reload
skip("Add assertions for updated state")
end
it "assigns the requested explore_menu as @explore_menu" do
explore_menu = ExploreMenu.create! valid_attributes
put :update, {:id => explore_menu.to_param, :explore_menu => valid_attributes}, valid_session
expect(assigns(:explore_menu)).to eq(explore_menu)
end
it "redirects to the explore_menu" do
explore_menu = ExploreMenu.create! valid_attributes
put :update, {:id => explore_menu.to_param, :explore_menu => valid_attributes}, valid_session
expect(response).to redirect_to(explore_menu)
end
end
context "with invalid params" do
it "assigns the explore_menu as @explore_menu" do
explore_menu = ExploreMenu.create! valid_attributes
put :update, {:id => explore_menu.to_param, :explore_menu => invalid_attributes}, valid_session
expect(assigns(:explore_menu)).to eq(explore_menu)
end
it "re-renders the 'edit' template" do
explore_menu = ExploreMenu.create! valid_attributes
put :update, {:id => explore_menu.to_param, :explore_menu => invalid_attributes}, valid_session
expect(response).to render_template("edit")
end
end
end
describe "DELETE #destroy" do
it "destroys the requested explore_menu" do
explore_menu = ExploreMenu.create! valid_attributes
expect {
delete :destroy, {:id => explore_menu.to_param}, valid_session
}.to change(ExploreMenu, :count).by(-1)
end
it "redirects to the explore_menus list" do
explore_menu = ExploreMenu.create! valid_attributes
delete :destroy, {:id => explore_menu.to_param}, valid_session
expect(response).to redirect_to(explore_menus_url)
end
end
end
+15
View File
@@ -0,0 +1,15 @@
require 'rails_helper'
# Specs in this file have access to a helper object that includes
# the ExploreMenusHelper. For example:
#
# describe ExploreMenusHelper do
# describe "string concat" do
# it "concats two strings with spaces" do
# expect(helper.concat_strings("this","that")).to eq("this that")
# end
# end
# end
RSpec.describe ExploreMenusHelper, type: :helper do
pending "add some examples to (or delete) #{__FILE__}"
end
+5
View File
@@ -0,0 +1,5 @@
require 'rails_helper'
RSpec.describe ExploreMenu, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end
+10
View File
@@ -0,0 +1,10 @@
require 'rails_helper'
RSpec.describe "ExploreMenus", type: :request do
describe "GET /explore_menus" do
it "works! (now write some real specs)" do
get explore_menus_path
expect(response).to have_http_status(200)
end
end
end
@@ -0,0 +1,39 @@
require "rails_helper"
RSpec.describe ExploreMenusController, type: :routing do
describe "routing" do
it "routes to #index" do
expect(:get => "/explore_menus").to route_to("explore_menus#index")
end
it "routes to #new" do
expect(:get => "/explore_menus/new").to route_to("explore_menus#new")
end
it "routes to #show" do
expect(:get => "/explore_menus/1").to route_to("explore_menus#show", :id => "1")
end
it "routes to #edit" do
expect(:get => "/explore_menus/1/edit").to route_to("explore_menus#edit", :id => "1")
end
it "routes to #create" do
expect(:post => "/explore_menus").to route_to("explore_menus#create")
end
it "routes to #update via PUT" do
expect(:put => "/explore_menus/1").to route_to("explore_menus#update", :id => "1")
end
it "routes to #update via PATCH" do
expect(:patch => "/explore_menus/1").to route_to("explore_menus#update", :id => "1")
end
it "routes to #destroy" do
expect(:delete => "/explore_menus/1").to route_to("explore_menus#destroy", :id => "1")
end
end
end
@@ -0,0 +1,27 @@
require 'rails_helper'
RSpec.describe "explore_menus/edit", type: :view do
before(:each) do
@explore_menu = assign(:explore_menu, ExploreMenu.create!(
:name => "MyString",
:resource_url => "MyString",
:icon_url => "MyString",
:description => "MyString"
))
end
it "renders the edit explore_menu form" do
render
assert_select "form[action=?][method=?]", explore_menu_path(@explore_menu), "post" do
assert_select "input#explore_menu_name[name=?]", "explore_menu[name]"
assert_select "input#explore_menu_resource_url[name=?]", "explore_menu[resource_url]"
assert_select "input#explore_menu_icon_url[name=?]", "explore_menu[icon_url]"
assert_select "input#explore_menu_description[name=?]", "explore_menu[description]"
end
end
end
@@ -0,0 +1,28 @@
require 'rails_helper'
RSpec.describe "explore_menus/index", type: :view do
before(:each) do
assign(:explore_menus, [
ExploreMenu.create!(
:name => "Name",
:resource_url => "Resource Url",
:icon_url => "Icon Url",
:description => "Description"
),
ExploreMenu.create!(
:name => "Name",
:resource_url => "Resource Url",
:icon_url => "Icon Url",
:description => "Description"
)
])
end
it "renders a list of explore_menus" do
render
assert_select "tr>td", :text => "Name".to_s, :count => 2
assert_select "tr>td", :text => "Resource Url".to_s, :count => 2
assert_select "tr>td", :text => "Icon Url".to_s, :count => 2
assert_select "tr>td", :text => "Description".to_s, :count => 2
end
end
@@ -0,0 +1,27 @@
require 'rails_helper'
RSpec.describe "explore_menus/new", type: :view do
before(:each) do
assign(:explore_menu, ExploreMenu.new(
:name => "MyString",
:resource_url => "MyString",
:icon_url => "MyString",
:description => "MyString"
))
end
it "renders new explore_menu form" do
render
assert_select "form[action=?][method=?]", explore_menus_path, "post" do
assert_select "input#explore_menu_name[name=?]", "explore_menu[name]"
assert_select "input#explore_menu_resource_url[name=?]", "explore_menu[resource_url]"
assert_select "input#explore_menu_icon_url[name=?]", "explore_menu[icon_url]"
assert_select "input#explore_menu_description[name=?]", "explore_menu[description]"
end
end
end
@@ -0,0 +1,20 @@
require 'rails_helper'
RSpec.describe "explore_menus/show", type: :view do
before(:each) do
@explore_menu = assign(:explore_menu, ExploreMenu.create!(
:name => "Name",
:resource_url => "Resource Url",
:icon_url => "Icon Url",
:description => "Description"
))
end
it "renders attributes in <p>" do
render
expect(rendered).to match(/Name/)
expect(rendered).to match(/Resource Url/)
expect(rendered).to match(/Icon Url/)
expect(rendered).to match(/Description/)
end
end