Permalink
Browse files

controller/view for categories

  • Loading branch information...
1 parent f158434 commit 33e6c590f2d1fe3e07de713aaf9c7d3b04ce6139 @yocha committed May 1, 2012
View
3 app/assets/javascripts/categories.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/
View
3 app/assets/javascripts/shopping_items.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/
View
3 app/assets/stylesheets/categories.css.scss
@@ -0,0 +1,3 @@
+// Place all the styles related to the categories controller here.
+// They will automatically be included in application.css.
+// You can use Sass (SCSS) here: http://sass-lang.com/
View
3 app/assets/stylesheets/shopping_items.css.scss
@@ -0,0 +1,3 @@
+// Place all the styles related to the shopping_items controller here.
+// They will automatically be included in application.css.
+// You can use Sass (SCSS) here: http://sass-lang.com/
View
8 app/controllers/application_controller.rb
@@ -1,3 +1,11 @@
class ApplicationController < ActionController::Base
protect_from_forgery
+
+ def check_admin
+ unless current_user.admin?
+ flash[:notice] = "Sorry! You don't have access."
+ redirect_to(root_path)
+ end
+ end
+
end
View
62 app/controllers/categories_controller.rb
@@ -0,0 +1,62 @@
+class CategoriesController < ApplicationController
+ before_filter :authenticate_user!
+ before_filter :check_admin, :except => [:index, :show]
+ def index
+ @categories = Category.all
+ respond_to do |format|
+ format.html
+ format.json {render json: @categories}
+ end
+ end
+ def show
+ @category = Category.find(params[:id])
+ respond_to do |format|
+ format.html
+ format.json {render json: @category}
+ end
+ end
+ def new
+ @category = Category.new
+ respond_to do |format|
+ format.html
+ format.json {render json: @category}
+ end
+ end
+ def edit
+ @category = Category.find(params[:id])
+ end
+ def create
+ @category = Category.new(params[:category])
+ respond_to do |format|
+ if @category.save
+ format.html {redirect_to @category, notice: 'Category was successfully created.'}
+ format.json {render json: @category, status: :created, location: @category}
+ else
+ format.html { render action: "new" }
+ format.json { render json: @category.errors, status: :unprocessable_entity }
+ end
+ end
+ end
+ def update
+ @category = Category.find(params[:id])
+
+ respond_to do |format|
+ if @category.update_attributes(params[:category])
+ format.html { redirect_to @category, notice: 'Category was successfully updated.' }
+ format.json { head :no_content }
+ else
+ format.html { render action: "edit" }
+ format.json { render json: @category.errors, status: :unprocessable_entity }
+ end
+ end
+ end
+ def destroy
+ @category = Category.find(params[:id])
+ @category.destroy
+
+ respond_to do |format|
+ format.html { redirect_to categories_url }
+ format.json { head :no_content }
+ end
+ end
+end
View
62 app/controllers/shopping_items_controller.rb
@@ -0,0 +1,62 @@
+class ShoppingItemsController < ApplicationController
+ before_filter :authenticate_user!
+ before_filter :check_admin, :except => [:index]
+ def index
+ @categories = Categories.all
+ respond_to do |format|
+ format.html
+ format.json {render json: @categories}
+ end
+ end
+ def show
+ @category = Category.find(params[:id])
+ respond_to do |format|
+ format.html
+ format.json {render json: @category}
+ end
+ end
+ def new
+ @category = Category.new
+ respond_to do |format|
+ format.html
+ format.json {render json: @category}
+ end
+ end
+ def edit
+ @category = Category.find(params[:id])
+ end
+ def create
+ @category = Category.new(params[:category])
+ respond_to do |format|
+ if @categorysave
+ format.html {redirect_to @category, notice: 'Category was successfully created.'}
+ format.json {render json: @category, status: :created, location: @category}
+ else
+ format.html { render action: "new" }
+ format.json { render json: @category.errors, status: :unprocessable_entity }
+ end
+ end
+ end
+ def update
+ @category = Category.find(params[:id])
+
+ respond_to do |format|
+ if @category.update_attributes(params[:category])
+ format.html { redirect_to @category, notice: 'Category was successfully updated.' }
+ format.json { head :no_content }
+ else
+ format.html { render action: "edit" }
+ format.json { render json: @category.errors, status: :unprocessable_entity }
+ end
+ end
+ end
+ def destroy
+ @category = Category.find(params[:id])
+ @category.destroy
+
+ respond_to do |format|
+ format.html { redirect_to category }
+ format.json { head :no_content }
+ end
+ end
+end
View
8 app/controllers/tasks_controller.rb
@@ -1,13 +1,7 @@
class TasksController < ApplicationController
+ before_filter :authenticate_user!
before_filter :check_admin, :except => [:index]
- def check_admin
- unless current_user.admin?
- flash[:notice] = "Sorry! You don't have access."
- redirect_to(root_path)
- end
- end
-
def index
@tasks = Task.all
respond_to do |format|
View
2 app/helpers/categories_helper.rb
@@ -0,0 +1,2 @@
+module CategoriesHelper
+end
View
2 app/helpers/shopping_items_helper.rb
@@ -0,0 +1,2 @@
+module ShoppingItemsHelper
+end
View
1 app/models/category.rb
@@ -1,2 +1,3 @@
class Category < ActiveRecord::Base
+ has_many :shopping_items
end
View
1 app/models/shopping_item.rb
@@ -1,2 +1,3 @@
class ShoppingItem < ActiveRecord::Base
+ belongs_to :category
end
View
20 app/views/categories/_form.html.erb
@@ -0,0 +1,20 @@
+<%= form_for @category, :html => { :class => 'form-horizontal' } do |f| %>
+ <div class="control-group">
+ <%= f.label :name, :class => 'control-label' %>
+ <div class="controls">
+ <%= f.text_area :name, :class => 'text_area' %>
+ </div>
+ </div>
+ <div class="control-group">
+ <%= f.label :description, :class => 'control-label' %>
+ <div class="controls">
+ <%= f.text_area :description, :class => 'text_area' %>
+ </div>
+ </div>
+
+ <div class="form-actions">
+ <%= f.submit nil, :class => 'btn btn-primary' %>
+ <%= link_to t('.cancel', :default => t("helpers.links.cancel")),
+ categories_path, :class => 'btn' %>
+ </div>
+<% end %>
View
4 app/views/categories/edit.html.erb
@@ -0,0 +1,4 @@
+<%- model_class = @category.class -%>
+<h1><%=t '.title', :default => t('helpers.titles.edit', :model => model_class.model_name.human,
+ :default => "Edit #{model_class.model_name.human}") %></h1>
+<%= render :partial => 'form' %>
View
34 app/views/categories/index.html.erb
@@ -0,0 +1,34 @@
+<%- model_class = Category.new.class -%>
+<h1><%=t '.title', :default => model_class.model_name.human.pluralize %></h1>
+<table class="table table-striped">
+ <thead>
+ <tr>
+ <th><%= model_class.human_attribute_name(:id) %></th>
+ <th><%= model_class.human_attribute_name(:name) %></th>
+ <th><%= model_class.human_attribute_name(:description) %></th>
+ <th><%=t '.actions', :default => t("helpers.actions") %></th>
+ </tr>
+ </thead>
+ <tbody>
+ <% @categories.each do |category| %>
+ <tr>
+ <td><%= link_to category.id, category_path(category) %></td>
+ <td><%= category.name %></td>
+ <td><%= category.description %></td>
+ <td>
+ <%= link_to t('.edit', :default => t("helpers.links.edit")),
+ edit_category_path(category), :class => 'btn btn-mini' %>
+ <%= link_to t('.destroy', :default => t("helpers.links.destroy")),
+ category_path(category),
+ :method => :delete,
+ :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')),
+ :class => 'btn btn-mini btn-danger' %>
+ </td>
+ </tr>
+ <% end %>
+ </tbody>
+</table>
+
+<%= link_to t('.new', :default => t("helpers.links.new")),
+ new_category_path,
+ :class => 'btn btn-primary' %>
View
4 app/views/categories/new.html.erb
@@ -0,0 +1,4 @@
+<%- model_class = @category.class -%>
+<h1><%=t '.title', :default => t('helpers.titles.new', :model => model_class.model_name.human,
+ :default => "New #{model_class.model_name.human}") %></h1>
+<%= render :partial => 'form' %>
View
23 app/views/categories/show.erb.html
@@ -0,0 +1,23 @@
+<%- model_class = @category.class -%>
+<h1><%=t '.title', :default => model_class.model_name.human %></h1>
+
+<p>
+ <strong><%= model_class.human_attribute_name(:name) %>:</strong><br>
+ <%= @category.name %>
+</p>
+<p>
+ <strong><%= model_class.human_attribute_name(:description) %>:</strong><br>
+ <%= @category.description %>
+</p>
+
+<div class="form-actions">
+ <%= link_to t('.back', :default => t("helpers.links.back")),
+ categories_path, :class => 'btn' %>
+ <%= link_to t('.edit', :default => t("helpers.links.edit")),
+ edit_category_path(@category), :class => 'btn' %>
+ <%= link_to t('.destroy', :default => t("helpers.links.destroy")),
+ category_path(@category),
+ :method => 'delete',
+ :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')),
+ :class => 'btn btn-danger' %>
+</div>
View
23 app/views/categories/show.html.erb
@@ -0,0 +1,23 @@
+<%- model_class = @category.class -%>
+<h1><%=t '.title', :default => model_class.model_name.human %></h1>
+
+<p>
+ <strong><%= model_class.human_attribute_name(:name) %>:</strong><br>
+ <%= @category.name %>
+</p>
+<p>
+ <strong><%= model_class.human_attribute_name(:description) %>:</strong><br>
+ <%= @category.description %>
+</p>
+
+<div class="form-actions">
+ <%= link_to t('.back', :default => t("helpers.links.back")),
+ categories_path, :class => 'btn' %>
+ <%= link_to t('.edit', :default => t("helpers.links.edit")),
+ edit_category_path(@category), :class => 'btn' %>
+ <%= link_to t('.destroy', :default => t("helpers.links.destroy")),
+ category_path(@category),
+ :method => 'delete',
+ :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')),
+ :class => 'btn btn-danger' %>
+</div>
View
2 config/routes.rb
@@ -3,6 +3,8 @@
resources :events
resources :tasks
+ resources :categories
+ resources :shopping_items
# The priority is based upon order of creation:
# first created -> highest priority.
View
2 db/migrate/20120501044812_create_shopping_items.rb
@@ -5,7 +5,7 @@ def change
t.integer :category_id
t.text :description
t.integer :suggested_quantity
- t.boolean :required
+ t.boolean :required, :default => false
t.timestamps
end
View
19 db/schema.rb
@@ -11,7 +11,14 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20120429064445) do
+ActiveRecord::Schema.define(:version => 20120501044942) do
+
+ create_table "categories", :force => true do |t|
+ t.string "name"
+ t.text "description"
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ end
create_table "events", :force => true do |t|
t.integer "task_id"
@@ -23,6 +30,16 @@
t.integer "user_id"
end
+ create_table "shopping_items", :force => true do |t|
+ t.string "name"
+ t.integer "category_id"
+ t.text "description"
+ t.integer "suggested_quantity"
+ t.boolean "required", :default => false
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ end
+
create_table "tasks", :force => true do |t|
t.string "name"
t.datetime "created_at", :null => false
View
5 db/seeds.rb
@@ -12,3 +12,8 @@
Task.create(name: "Feed")
Event.delete_all
Event.create(task_id: Task.first.id, user_id: User.first.id, start_time: Time.now, duration: 30, comment: "easy")
+
+Category.delete_all
+Category.create(:name => 'stuff')
+ShoppingItem.delete_all
+ShoppingItem.create(:name => 'bottles', :category_id => Category.first.id)
View
5 spec/controllers/categories_controller_spec.rb
@@ -0,0 +1,5 @@
+require 'spec_helper'
+
+describe CategoriesController do
+
+end
View
5 spec/controllers/shopping_items_controller_spec.rb
@@ -0,0 +1,5 @@
+require 'spec_helper'
+
+describe ShoppingItemsController do
+
+end
View
15 spec/helpers/categories_helper_spec.rb
@@ -0,0 +1,15 @@
+require 'spec_helper'
+
+# Specs in this file have access to a helper object that includes
+# the CategoriesHelper. For example:
+#
+# describe CategoriesHelper do
+# describe "string concat" do
+# it "concats two strings with spaces" do
+# helper.concat_strings("this","that").should == "this that"
+# end
+# end
+# end
+describe CategoriesHelper do
+ pending "add some examples to (or delete) #{__FILE__}"
+end
View
15 spec/helpers/shopping_items_helper_spec.rb
@@ -0,0 +1,15 @@
+require 'spec_helper'
+
+# Specs in this file have access to a helper object that includes
+# the ShoppingItemsHelper. For example:
+#
+# describe ShoppingItemsHelper do
+# describe "string concat" do
+# it "concats two strings with spaces" do
+# helper.concat_strings("this","that").should == "this that"
+# end
+# end
+# end
+describe ShoppingItemsHelper do
+ pending "add some examples to (or delete) #{__FILE__}"
+end

0 comments on commit 33e6c59

Please sign in to comment.