Here ya go, huntington...

master
Matthew F. Short 10 years ago
parent e57c97bf3b
commit 3c1e821fab

@ -1,7 +1,7 @@
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
protect_from_forgery with: :null_session
helper_method :current_user
@ -9,19 +9,13 @@ class ApplicationController < ActionController::Base
render '/welcome'
end
def amiloggedin
amiloggedin = !!session[:current_user_id]
render json: current_user
end
private
def current_user
if session[:current_user_id]
@current_user ||= User.find(session[:current_user_id])
else
@current_user = false
@current_user = nil
end
end

@ -1,2 +1,26 @@
class ConfessionsController < ApplicationController
# skip_before_action :verify_authenticity_token
def create
transgression = Transgression.find(params[:transgression_id])
@confession = transgression.confessions.new(confession_params)
if @confession.save
else
render json: {
error: {
message: @confession.errors.full_messages.to_sentence
}
}
end
end
private
def confession_params
return params.require(:confession)
.permit(:description, :occurred_at)
end
end

@ -1,4 +1,5 @@
class SessionController < ApplicationController
# skip_before_action :verify_authenticity_token, only: :create
def create
user = User.find_by(email: user_params[:email])
@ -19,6 +20,11 @@ class SessionController < ApplicationController
redirect_to root_path
end
def current_sinner
end
private
def user_params

@ -1,52 +1,29 @@
class TransgressionsController < ApplicationController
before_action :require_current_user
# skip_before_action :verify_authenticity_token, only: :create
def new
@transgression = Transgression.new
def index
@transgressions = current_user.transgressions.includes(:confessions)
end
def create
@transgression = current_user.transgressions
.new(transgression_params)
@transgression = current_user.transgressions.new(transgression_params)
@confession = @transgression.confessions.new(confession_params)
if @transgression.save
if @transgression.save && @confession.save
redirect_to transgressions_path
else
flash[:message] = @transgression.errors.full_messages.to_sentence
render :new
render json: {
error: {
message: @transgression.errors.full_messages.to_sentence
}
}
end
end
def edit
end
def update
# params[:id]
end
def show
end
def index
@transgressions = current_user.transgressions
.includes(:confessions)
.group_by { |x| x.sin_type }
end
def delete
end
private
def transgression_params
return params.require(:transgression)
.permit(:sin_type, :description)
end
def confession_params
return params.require(:confession)
.permit(:description, :occurred_at)
end
end

@ -0,0 +1,7 @@
json.transgression_id @confession.transgression_id
json.confession do
json.id @confession.id
json.description @confession.description
json.occurred_at time_ago_in_words(@confession.created_at) + " ago"
end

@ -5,13 +5,6 @@
The Sins We Commit
</span>
</div>
<nav>
<% if current_user %>
<a href="<%= new_transgression_path %>">
Please me, mortal!
</a>
<% end %>
</nav>
</header>
<%= javascript_include_tag 'login_signup.js' %>

@ -0,0 +1,7 @@
if current_user
json.current_user do
json.email current_user.email
end
else
json.current_user nil
end

@ -0,0 +1,7 @@
json.transgression do
json.id @transgression.id
json.sin_type @transgression.sin_type
json.description @transgression.description
json.authored_at time_ago_in_words(@transgression.created_at) + " ago"
json.confessions []
end

@ -1,22 +0,0 @@
<% @transgressions.each do |sin_type, trans| %>
<h4>
For the sin of <em><%= sin_type %></em>, your weaknesses are:
</h4>
<ul>
<% trans.each do |t| %>
<li>
<%= t.description %>, with <%= t.confessions.count %> confessions:
<% if t.confessions.count > 0 %>
<ul>
<% t.confessions.each do |c| %>
<li>
"<%= c.description %>" - <%= c.occurred_at %>
</li>
<% end %>
</ul>
<% end %>
</li>
<% end %>
</ul>
<% end %>

@ -0,0 +1,15 @@
json.sinner current_user.email
json.transgressions(@transgressions) do |trans|
json.id trans.id
json.sin_type trans.sin_type
json.description trans.description
json.authored_at time_ago_in_words(trans.created_at) + " ago"
json.confessions(trans.confessions) do |conf|
json.id conf.id
json.description conf.description
json.occurred_at time_ago_in_words(conf.created_at) + " ago"
end
end

@ -1,21 +0,0 @@
<form action="<%= transgressions_path %>" method="POST"
class='transgression'>
<input type="hidden" name="authenticity_token"
value="<%= form_authenticity_token %>">
<input type="text" name="transgression[description]"
placeholder="Weaknesses are beautiful..."
value="<%= @transgression.description %>"><br/>
<select name="transgression[sin_type]">
<% Transgression::SIN_TYPES.each do |sin| %>
<option value="<%= sin %>"
<%= @transgression.sin_type == sin ? "selected" : "" %>>
<%= sin %>
</option>
<% end %>
</select>
<hr/>
<h5>Your first confession!</h5>
<input type="text" name="confession[description]"><br/>
<input type="date" name="confession[occurred_at]"><br/>
<input type="submit" value="Here I am">
</form>

@ -1,20 +1,19 @@
Rails.application.routes.draw do
resources :confessions
resources :transgressions
root 'application#welcome'
get '/amiloggedin' => 'application#amiloggedin'
resources :transgressions, only: [:index, :create], defaults: { format: :json } do
resources :confessions, only: [:create], shallow: true
end
# resources :users, only: [:create]
post '/users' => 'users#create'
# sessiony stuff
# get '/session' => 'session#current_user'
# angular?
# session stuff
get '/session' => 'session#current_sinner', defaults: { format: :json }
post '/session' => 'session#create'
delete '/session' => 'session#destroy'
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".

Loading…
Cancel
Save