Skip to content

Commit

Permalink
Added crud
Browse files Browse the repository at this point in the history
  • Loading branch information
darko1002001 committed May 17, 2013
1 parent c67d64a commit 3b81308
Show file tree
Hide file tree
Showing 32 changed files with 564 additions and 296 deletions.
9 changes: 4 additions & 5 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
source 'https://rubygems.org'

gem 'rails', '3.2.11'

# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'

gem 'pg'


Expand All @@ -29,7 +25,10 @@ gem 'jquery-rails'
# gem 'jbuilder'

# Use unicorn as the app server
# gem 'unicorn'
gem 'unicorn'

gem 'rabl'
gem 'oj'

# Deploy with Capistrano
# gem 'capistrano'
Expand Down
12 changes: 12 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,18 @@ GEM
railties (>= 3.0, < 5.0)
thor (>= 0.14, < 2.0)
json (1.8.0)
kgio (2.8.0)
mail (2.4.4)
i18n (>= 0.4.0)
mime-types (~> 1.16)
treetop (~> 1.4.8)
mime-types (1.23)
multi_json (1.7.3)
oj (2.0.7)
pg (0.15.1)
polyglot (0.3.3)
rabl (0.8.0)
activesupport (>= 2.3.14)
rack (1.4.5)
rack-cache (1.2)
rack (>= 0.4)
Expand All @@ -77,6 +81,7 @@ GEM
rake (>= 0.8.7)
rdoc (~> 3.4)
thor (>= 0.14.6, < 2.0)
raindrops (0.10.0)
rake (10.0.4)
rdoc (3.12.2)
json (~> 1.4)
Expand All @@ -99,14 +104,21 @@ GEM
uglifier (2.1.0)
execjs (>= 0.3.0)
multi_json (~> 1.0, >= 1.0.2)
unicorn (4.6.2)
kgio (~> 2.6)
rack
raindrops (~> 0.7)

PLATFORMS
ruby

DEPENDENCIES
coffee-rails (~> 3.2.1)
jquery-rails
oj
pg
rabl
rails (= 3.2.11)
sass-rails (~> 3.2.3)
uglifier (>= 1.0.3)
unicorn
3 changes: 3 additions & 0 deletions app/assets/javascripts/categories.js.coffee
Original file line number Diff line number Diff line change
@@ -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 changes: 3 additions & 0 deletions app/assets/javascripts/notes.js.coffee
Original file line number Diff line number Diff line change
@@ -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 changes: 3 additions & 0 deletions app/assets/stylesheets/categories.css.scss
Original file line number Diff line number Diff line change
@@ -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/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/notes.css.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the Notes controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
69 changes: 69 additions & 0 deletions app/assets/stylesheets/scaffolds.css.scss
Original file line number Diff line number Diff line change
@@ -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;
}
}
7 changes: 7 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
class ApplicationController < ActionController::Base
protect_from_forgery


rescue_from ActiveRecord::RecordNotFound, :with => :record_not_found

def record_not_found
render json: {:error => "Unable to find item"} , status: :unprocessable_entity
end
end
83 changes: 83 additions & 0 deletions app/controllers/categories_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
class CategoriesController < ApplicationController
# GET /categories
# GET /categories.json
def index
@categories = Category.all

respond_to do |format|
format.html # index.html.erb
format.json { render json: @categories }
end
end

# GET /categories/1
# GET /categories/1.json
def show
@category = Category.find(params[:id])

respond_to do |format|
format.html # show.html.erb
format.json { render json: @category }
end
end

# GET /categories/new
# GET /categories/new.json
def new
@category = Category.new

respond_to do |format|
format.html # new.html.erb
format.json { render json: @category }
end
end

# GET /categories/1/edit
def edit
@category = Category.find(params[:id])
end

# POST /categories
# POST /categories.json
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

# PUT /categories/1
# PUT /categories/1.json
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

# DELETE /categories/1
# DELETE /categories/1.json
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
92 changes: 92 additions & 0 deletions app/controllers/notes_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
class NotesController < ApplicationController

before_filter :load_category

# GET /notes
# GET /notes.json
def index
@notes = @category.notes.all

respond_to do |format|
format.html # index.html.erb
format.json { render json: @notes }
end
end

# GET /notes/1
# GET /notes/1.json
def show
@note = Note.find(params[:id])

respond_to do |format|
format.html # show.html.erb
format.json { render json: @note }
end
end

# GET /notes/new
# GET /notes/new.json
def new
@note = @category.notes.new

respond_to do |format|
format.html # new.html.erb
format.json { render json: @note }
end
end

# GET /notes/1/edit
def edit
@note = Note.find(params[:id])
end

# POST /notes
# POST /notes.json
def create
@note = @category.notes.new(params[:note])

respond_to do |format|
if @note.save
format.html { redirect_to category_notes_path, notice: 'Note was successfully created.' }
format.json { render json: @note, status: :created, location: @note }
else
format.html { render action: "new" }
format.json { render json: @note.errors, status: :unprocessable_entity }
end
end
end

# PUT /notes/1
# PUT /notes/1.json
def update
@note = Note.find(params[:id])

respond_to do |format|
if @note.update_attributes(params[:note])
format.html { redirect_to @note, notice: 'Note was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @note.errors, status: :unprocessable_entity }
end
end
end

# DELETE /notes/1
# DELETE /notes/1.json
def destroy
@note = Note.find(params[:id])
@note.destroy

respond_to do |format|
format.html { redirect_to notes_url }
format.json { head :no_content }
end
end


private
def load_category
@category = Category.find(params[:category_id])
end
end
2 changes: 2 additions & 0 deletions app/helpers/categories_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module CategoriesHelper
end
2 changes: 2 additions & 0 deletions app/helpers/notes_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module NotesHelper
end
5 changes: 5 additions & 0 deletions app/models/category.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Category < ActiveRecord::Base
attr_accessible :description, :name, :title
has_many :notes

end
5 changes: 5 additions & 0 deletions app/models/note.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Note < ActiveRecord::Base
attr_accessible :description, :name, :title

belongs_to :category
end
29 changes: 29 additions & 0 deletions app/views/categories/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<%= form_for(@category) do |f| %>
<% if @category.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@category.errors.count, "error") %> prohibited this category from being saved:</h2>

<ul>
<% @category.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>

<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :description %><br />
<%= f.text_area :description %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Loading

0 comments on commit 3b81308

Please sign in to comment.