Skip to content

Commit

Permalink
added post controller
Browse files Browse the repository at this point in the history
  • Loading branch information
enterteg committed Mar 23, 2016
1 parent 54a1387 commit 8dc3352
Show file tree
Hide file tree
Showing 47 changed files with 505 additions and 61 deletions.
5 changes: 3 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
gem 'haml'
gem 'bcrypt', '~> 3.1.7'
gem 'haml', '~> 4.0', '>= 4.0.7'
gem 'paperclip', '~> 4.3', '>= 4.3.6'
# Use Unicorn as the app server
# gem 'unicorn'

Expand Down
16 changes: 15 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,15 @@ GEM
thread_safe (~> 0.3, >= 0.3.4)
tzinfo (~> 1.1)
arel (6.0.3)
bcrypt (3.1.11)
binding_of_caller (0.7.2)
debug_inspector (>= 0.0.1)
builder (3.2.2)
byebug (8.2.2)
climate_control (0.0.3)
activesupport (>= 3.0)
cocaine (0.5.8)
climate_control (>= 0.0.3, < 1.0)
coffee-rails (4.1.1)
coffee-script (>= 2.2.0)
railties (>= 4.0.0, < 5.1.x)
Expand Down Expand Up @@ -70,11 +75,18 @@ GEM
mail (2.6.3)
mime-types (>= 1.16, < 3)
mime-types (2.99.1)
mimemagic (0.3.0)
mini_portile2 (2.0.0)
minitest (5.8.4)
multi_json (1.11.2)
nokogiri (1.6.7.2)
mini_portile2 (~> 2.0.0.rc2)
paperclip (4.3.6)
activemodel (>= 3.2.0)
activesupport (>= 3.2.0)
cocaine (~> 0.5.5)
mime-types
mimemagic (= 0.3.0)
rack (1.6.4)
rack-test (0.6.3)
rack (>= 1.0)
Expand Down Expand Up @@ -144,11 +156,13 @@ PLATFORMS
ruby

DEPENDENCIES
bcrypt (~> 3.1.7)
byebug
coffee-rails (~> 4.1.0)
haml
haml (~> 4.0, >= 4.0.7)
jbuilder (~> 2.0)
jquery-rails
paperclip (~> 4.3, >= 4.3.6)
rails (= 4.2.6)
sass-rails (~> 5.0)
sdoc (~> 0.4.0)
Expand Down
7 changes: 7 additions & 0 deletions app/assets/javascripts/posts.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
$ ->
$('#post_category_id').on 'change', ->
$.ajax 'update_form',
type: 'POST',
data: {
category_id: $('#post_category_id option:selected').val()
}
1 change: 1 addition & 0 deletions app/assets/javascripts/public.coffee.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ $(document).ready ->
refresh_pin()
)


refresh_pin = ->
$('#location_pin').remove()
$('#map_photo').off('click')
Expand Down
3 changes: 3 additions & 0 deletions app/assets/javascripts/session.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://coffeescript.org/
2 changes: 2 additions & 0 deletions app/assets/stylesheets/mixins.sass
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
@content
@keyframes $name
@content
=animation($anime)
-webkit-animation: $anime

=transform($transforms)
-moz-transform: $transforms
Expand Down
3 changes: 3 additions & 0 deletions app/assets/stylesheets/posts.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the Posts controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
77 changes: 67 additions & 10 deletions app/assets/stylesheets/public.sass
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
@import 'mixins'

$button-color: rgba(150,0,250,0.1)
$top_menu_height: 80px
body
margin: 0
padding: 0
font-family: 'Roboto', sans-serif
font-size: 16px
.container
max-width: 1100px
max-width: 1000px
margin: 0 auto
background: white

padding: 0px 10px
#top_menu
float: left
height: $top_menu_height
Expand All @@ -23,24 +24,49 @@ body
float: left
margin: 0px 15px
ul
color: rgba(250,20,250,1)
padding: 0px
margin: 0px
line-height: $top_menu_height
float: left
list-style-type: none
li
position: relative
font-size: 1.2rem
color: black
font-size: 1.3rem
display: inline
margin: 0px 2px
padding: 3px 5px
border: 0px solid white
+transition(0.2s ease all)
top: 0px
border-top: 0px solid white
+transition(0.5s border)
&:hover
top: -2px
border-bottom: 0.5px solid #ccc
position: relative
cursor: pointer
border-top: 30px solid rgba(150,0,200,0.1)
.admin
float: right
line-height: $top_menu_height
.button
border-radius: 7px
margin-left: 5px
padding: 7px 15px
text-decoration: none
color: #555
+transition(0.3s ease all)
background-color: rgba(150,0,150,0.05)
&:hover
background-color: rgba(140,0,150,0.1)
color: #333
&.logout
outline: none
background-color: transparent
padding: 0px
font-size: 0.9e
padding: 5px 10px
border-bottom: 1px solid white
&:hover
border-bottom: 1px solid #ccc


.social
float: right
height: $top_menu_height
Expand All @@ -60,6 +86,35 @@ body
&:hover
opacity: 0.7

section
margin: 100px 0px
.post
max-width: 600px
margin: 0 auto
font-family: 'Raleway'
header
margin-left: 10px
font-size: 2rem
footer
font-size: 0.9rem
line-height: 2
&:before
content: ''
width: 80%
height: 1px
display: inline-block
background: #ccc
position: relative
top: -4px
margin-right: 10px
article

font-weigth: 200
padding: 15px 10px
width: 90%
text-align: justify
line-height: 1.4
font-size: 1.2rem

#logo_banner
clear: both
Expand All @@ -84,6 +139,9 @@ body
.bigger
letter-spacing: 5px
font-size: 5rem



//--------- map styles
#refresh_pin_button
Expand All @@ -99,7 +157,6 @@ body
z-index: 1
img
width: 100%
height: 100%
.map_pin
position: absolute
width: 20px
Expand Down
3 changes: 3 additions & 0 deletions app/assets/stylesheets/session.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the session controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
10 changes: 10 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,14 @@ class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception

private
def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
helper_method :current_user

def authenticate_user
redirect_to login_path if current_user.nil?
end
end
53 changes: 53 additions & 0 deletions app/controllers/posts_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
class PostsController < ApplicationController
before_action :find_post, only: [:show, :edit, :update, :destroy]

def new
@post = Post.new
end

def create
@post = Post.new(post_params)
if @post.save
redirect_to @post, notice: 'Post created successfully'
else
flash.now.notice = 'Some errors occured, cannot create post.'
render :new
end
end

def show
end
def edit
end

def update
if @post.update(post_params)
redirect_to @post, notice: 'Post updated successfully'
else
flash.now.notice = 'Some errors occured, cannot update post.'
render :edit
end
end

def destroy
if @post.destroy
redirect_to :root, notice: 'Post deleted successfully'
else
redirect_to @post, notice: 'Cannot delete post, some errors occured'
end
end
def update_form
@category_id = params[:category_id]

respond_to do |format|
format.js
end
end
private
def find_post
@post = Post.find(params[:id])
end
def post_params
params.require(:post).permit(:title,:desc,:category_id)
end
end
4 changes: 4 additions & 0 deletions app/controllers/public_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
class PublicController < ApplicationController
def index
end

def stats

end
end
22 changes: 22 additions & 0 deletions app/controllers/session_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class SessionController < ApplicationController
def new
redirect_to :root if current_user
end

def create
reset_session
user = User.find_by(login: params[:login])
if user && user.authenticate(params[:password])
session[:user_id] = user.id
redirect_to :root
else
flash.now.notice = 'Invalid login or password'
render :new
end
end

def destroy
reset_session
redirect_to :root
end
end
1 change: 1 addition & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
module ApplicationHelper

end
2 changes: 2 additions & 0 deletions app/helpers/posts_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module PostsHelper
end
2 changes: 2 additions & 0 deletions app/helpers/session_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module SessionHelper
end
3 changes: 3 additions & 0 deletions app/models/category.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Category < ActiveRecord::Base
has_many :posts
end
3 changes: 3 additions & 0 deletions app/models/photo.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Photo < ActiveRecord::Base
belongs_to :post
end
8 changes: 8 additions & 0 deletions app/models/post.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Post < ActiveRecord::Base
belongs_to :category
has_many :photos

validates_presence_of :title, :desc, :category_id
validates :desc, length: { minimum: 100}
validates :title, length: { minimum: 3}
end
3 changes: 3 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class User < ActiveRecord::Base
has_secure_password
end
15 changes: 0 additions & 15 deletions app/views/layouts/application.html.erb

This file was deleted.

Loading

0 comments on commit 8dc3352

Please sign in to comment.