diff --git a/.gitignore b/.gitignore index 5b61ab0..6db3c9a 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,5 @@ /log/* !/log/.keep /tmp + +.DS_Store diff --git a/README.md b/README.md index dd4e97e..d477840 100644 --- a/README.md +++ b/README.md @@ -1,28 +1,60 @@ -== README - -This README would normally document whatever steps are necessary to get the -application up and running. - -Things you may want to cover: - -* Ruby version - -* System dependencies - -* Configuration - -* Database creation - -* Database initialization - -* How to run the test suite - -* Services (job queues, cache servers, search engines, etc.) - -* Deployment instructions - -* ... - - -Please feel free to use a different markup language if you do not plan to run -rake doc:app. +# API Docs + +## Classical + +User + Session are all done the 'old fashioned way' via server-rendered erb. + +ROOT (/): Renders the Sign In / Sign Up forms. +POST /users will create a new user, redirect to ROOT +POST /session will create a new session, redirect to SPA page + +--- +## JSON + +GET /current_user will return JSON object containing currently logged in user + +GET /transgressions will return array of transgressions complete with confessions subarray + + ```json + { + transgressions: [ + { + id: 3, + sin_type: "Gluttony", + description: "I love eating a whole box full of cucumbers, like a 20lb box" + confessions: [ + { + description: "Some description", + occurred_at: "3 weeks ago" + } + ] + } + ] + } + ``` + +POST /transgressions will take a JSON object as such: + + ```json + { + authenticity_token: "osmeaksdfiohuiHDFIUSDHFUkjadfhjk324", + transgression: { + sin_type: "Some Sin", + description: "Some Description" + } + } + ``` + + And return the created transgression as an object. + +POST /transgressions/:id/confessions will take a JSON object as such: + + ```json + { + authenticity_token: "adsf123i478KJkhajksldfhjk0", + confession: { + description: "I did a thing", + occurred_at: "2015-03-27" + } + } + ``` diff --git a/app/.DS_Store b/app/.DS_Store deleted file mode 100644 index 84f5661..0000000 Binary files a/app/.DS_Store and /dev/null differ diff --git a/app/assets/javascripts/confessions.coffee b/app/assets/javascripts/confessions.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/app/assets/javascripts/confessions.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://coffeescript.org/ diff --git a/app/assets/stylesheets/confessions.scss b/app/assets/stylesheets/confessions.scss new file mode 100644 index 0000000..fc0467c --- /dev/null +++ b/app/assets/stylesheets/confessions.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the confessions controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/confessions_controller.rb b/app/controllers/confessions_controller.rb new file mode 100644 index 0000000..25107d4 --- /dev/null +++ b/app/controllers/confessions_controller.rb @@ -0,0 +1,2 @@ +class ConfessionsController < ApplicationController +end diff --git a/app/controllers/transgressions_controller.rb b/app/controllers/transgressions_controller.rb index 5f7b2ad..9b6ff2b 100644 --- a/app/controllers/transgressions_controller.rb +++ b/app/controllers/transgressions_controller.rb @@ -9,7 +9,9 @@ class TransgressionsController < ApplicationController @transgression = current_user.transgressions .new(transgression_params) - if @transgression.save + @confession = @transgression.confessions.new(confession_params) + + if @transgression.save && @confession.save redirect_to transgressions_path else flash[:message] = @transgression.errors.full_messages.to_sentence @@ -29,6 +31,8 @@ class TransgressionsController < ApplicationController def index @transgressions = current_user.transgressions + .includes(:confessions) + .group_by { |x| x.sin_type } end def delete @@ -40,4 +44,9 @@ class TransgressionsController < ApplicationController return params.require(:transgression) .permit(:sin_type, :description) end + + def confession_params + return params.require(:confession) + .permit(:description, :occurred_at) + end end diff --git a/app/helpers/confessions_helper.rb b/app/helpers/confessions_helper.rb new file mode 100644 index 0000000..0266769 --- /dev/null +++ b/app/helpers/confessions_helper.rb @@ -0,0 +1,2 @@ +module ConfessionsHelper +end diff --git a/app/models/confession.rb b/app/models/confession.rb new file mode 100644 index 0000000..49c719c --- /dev/null +++ b/app/models/confession.rb @@ -0,0 +1,8 @@ +class Confession < ActiveRecord::Base + validates :description, presence: true + validates :transgression, presence: true + validates :occurred_at, presence: true + + belongs_to :transgression + delegate :sinner, to: :transgression +end diff --git a/app/models/transgression.rb b/app/models/transgression.rb index 7f4c475..e47ff6b 100644 --- a/app/models/transgression.rb +++ b/app/models/transgression.rb @@ -5,10 +5,14 @@ class Transgression < ActiveRecord::Base ] validates :description, presence: true - validates :user, presence: true + validates :sinner, presence: true validates :sin_type, inclusion: { in: SIN_TYPES } - belongs_to :user + # belongs_to :sinner, it would assume there is a Sinner Class, as well as a sinner_id column in this table + + belongs_to :sinner, class_name: "User", foreign_key: :user_id + + has_many :confessions, dependent: :destroy end # Transgression::SIN_TYPES diff --git a/app/models/user.rb b/app/models/user.rb index 8e0dc61..0e76bb5 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -8,5 +8,6 @@ class User < ActiveRecord::Base minimum: 8, allow_nil: true } - has_many :transgressions + has_many :transgressions, dependent: :destroy + has_many :confessions, through: :transgressions end diff --git a/app/views/transgressions/index.html.erb b/app/views/transgressions/index.html.erb index f2add68..c252c40 100644 --- a/app/views/transgressions/index.html.erb +++ b/app/views/transgressions/index.html.erb @@ -1,3 +1,22 @@ -<% @transgressions.each do |trans| %> -
For the sin of <%= trans.sin_type %>: <%= trans.description %>
+ +<% @transgressions.each do |sin_type, trans| %> +

+ For the sin of <%= sin_type %>, your weaknesses are: +

+ <% end %> diff --git a/app/views/transgressions/new.html.erb b/app/views/transgressions/new.html.erb index be7b5af..3fbd28d 100644 --- a/app/views/transgressions/new.html.erb +++ b/app/views/transgressions/new.html.erb @@ -13,5 +13,9 @@ <% end %> +
+
Your first confession!
+
+
diff --git a/config/routes.rb b/config/routes.rb index b1b3cd6..7b58590 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,5 @@ Rails.application.routes.draw do + resources :confessions resources :transgressions root 'application#welcome' diff --git a/db/migrate/20151030132117_create_confessions.rb b/db/migrate/20151030132117_create_confessions.rb new file mode 100644 index 0000000..65edf24 --- /dev/null +++ b/db/migrate/20151030132117_create_confessions.rb @@ -0,0 +1,11 @@ +class CreateConfessions < ActiveRecord::Migration + def change + create_table :confessions do |t| + t.references :transgression, index: true, foreign_key: true + t.date :occured_at + t.string :description + + t.timestamps null: false + end + end +end diff --git a/db/migrate/20151030132525_change_column_in_confessions.rb b/db/migrate/20151030132525_change_column_in_confessions.rb new file mode 100644 index 0000000..18a5ccb --- /dev/null +++ b/db/migrate/20151030132525_change_column_in_confessions.rb @@ -0,0 +1,5 @@ +class ChangeColumnInConfessions < ActiveRecord::Migration + def change + rename_column :confessions, :occured_at, :occurred_at + end +end diff --git a/db/migrate/20151030132812_change_columns_in_confessions.rb b/db/migrate/20151030132812_change_columns_in_confessions.rb new file mode 100644 index 0000000..d8a3e6a --- /dev/null +++ b/db/migrate/20151030132812_change_columns_in_confessions.rb @@ -0,0 +1,6 @@ +class ChangeColumnsInConfessions < ActiveRecord::Migration + def change + change_column :confessions, :description, :string, null: false + change_column :confessions, :occurred_at, :datetime, null: false, default: Time.now + end +end diff --git a/db/schema.rb b/db/schema.rb index 0f3821c..ac2c50d 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,11 +11,21 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20151029180427) do +ActiveRecord::Schema.define(version: 20151030132812) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" + create_table "confessions", force: :cascade do |t| + t.integer "transgression_id" + t.datetime "occurred_at", default: '2015-11-02 14:55:24', null: false + t.string "description", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + add_index "confessions", ["transgression_id"], name: "index_confessions_on_transgression_id", using: :btree + create_table "transgressions", force: :cascade do |t| t.integer "user_id" t.string "description", null: false @@ -36,5 +46,6 @@ ActiveRecord::Schema.define(version: 20151029180427) do add_index "users", ["email"], name: "index_users_on_email", using: :btree + add_foreign_key "confessions", "transgressions" add_foreign_key "transgressions", "users" end diff --git a/db/seeds.rb b/db/seeds.rb index 4edb1e8..9637458 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -5,3 +5,11 @@ # # cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }]) # Mayor.create(name: 'Emanuel', city: cities.first) + +joe = User.find_or_initialize_by(email: "joeschmoe@joe.co") + +joe.update(password: "i<3potatoes") + +sniffing = Transgression.find_or_create_by(description: "I can't stop myself from sniffing other people's hair.", sin_type: "Lust", sinner: joe) + +confession = sniffing.confessions.create(description: "Creeped up behind someone in the subway.") diff --git a/test/controllers/confessions_controller_test.rb b/test/controllers/confessions_controller_test.rb new file mode 100644 index 0000000..6b41447 --- /dev/null +++ b/test/controllers/confessions_controller_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class ConfessionsControllerTest < ActionController::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/fixtures/confessions.yml b/test/fixtures/confessions.yml new file mode 100644 index 0000000..0fc0c6c --- /dev/null +++ b/test/fixtures/confessions.yml @@ -0,0 +1,11 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + transgression: + occurred_at: 2015-10-30 + description: MyString + +two: + transgression: + occurred_at: 2015-10-30 + description: MyString diff --git a/test/models/confession_test.rb b/test/models/confession_test.rb new file mode 100644 index 0000000..2f9d305 --- /dev/null +++ b/test/models/confession_test.rb @@ -0,0 +1,9 @@ +require 'test_helper' + +class ConfessionTest < ActiveSupport::TestCase + test "the truth" do + a = Confession.new + + assert_not a.save + end +end