From f8c03d6f5c7d893f85dc741c39318571d9b787f4 Mon Sep 17 00:00:00 2001 From: Mark Moser Date: Fri, 18 Sep 2015 15:50:52 -0500 Subject: [PATCH] app port from old version --- Gemfile | 2 + Gemfile.lock | 12 +++++ Guardfile | 12 +++++ app/controllers/application_controller.rb | 2 + app/controllers/children_controller.rb | 4 ++ app/controllers/staff_controller.rb | 4 ++ app/controllers/users_controller.rb | 4 ++ app/models/child.rb | 11 +++++ app/models/parenthood.rb | 4 ++ app/models/person.rb | 25 +++++++++++ app/views/children/index.haml | 8 ++++ app/views/parents/index.haml | 11 +++++ app/views/staff/index.haml | 4 ++ app/views/users/index.haml | 5 +++ config/database.yml | 2 +- config/environments/development.rb | 3 ++ db/schema.rb | 50 +++++++++++++++++++++ test/controllers/parents_controller_test.rb | 2 +- test/lib/secrets_test.rb | 8 ++-- 19 files changed, 167 insertions(+), 6 deletions(-) create mode 100644 app/models/child.rb create mode 100644 app/models/parenthood.rb create mode 100644 app/models/person.rb create mode 100644 app/views/children/index.haml create mode 100644 app/views/parents/index.haml create mode 100644 app/views/staff/index.haml create mode 100644 app/views/users/index.haml create mode 100644 db/schema.rb diff --git a/Gemfile b/Gemfile index 972275e..e9548ce 100644 --- a/Gemfile +++ b/Gemfile @@ -33,5 +33,7 @@ group :development, :test do gem 'guard' gem 'guard-rubocop' gem 'guard-minitest' + gem 'guard-livereload' + gem 'rack-livereload' gem 'minitest-reporters' end diff --git a/Gemfile.lock b/Gemfile.lock index 6909aed..61c5907 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -59,6 +59,9 @@ GEM columnize (0.9.0) daemons (1.2.3) debug_inspector (0.0.2) + em-websocket (0.5.1) + eventmachine (>= 0.12.9) + http_parser.rb (~> 0.6.0) erubis (2.7.0) eventmachine (1.0.8) execjs (2.6.0) @@ -76,6 +79,10 @@ GEM shellany (~> 0.0) thor (>= 0.18.1) guard-compat (1.2.1) + guard-livereload (2.4.0) + em-websocket (~> 0.5) + guard (~> 2.8) + multi_json (~> 1.8) guard-minitest (2.4.4) guard-compat (~> 1.2) minitest (>= 3.0) @@ -95,6 +102,7 @@ GEM haml (~> 4.0.0) nokogiri (~> 1.6.0) ruby_parser (~> 3.5) + http_parser.rb (0.6.0) i18n (0.7.0) jquery-rails (4.0.5) rails-dom-testing (~> 1.0) @@ -140,6 +148,8 @@ GEM pry-rails (0.3.4) pry (>= 0.9.10) rack (1.6.4) + rack-livereload (0.3.16) + rack rack-test (0.6.3) rack (>= 1.0) rails (4.2.4) @@ -231,6 +241,7 @@ DEPENDENCIES bcrypt (~> 3.1.7) binding_of_caller guard + guard-livereload guard-minitest guard-rubocop haml-rails (~> 0.9) @@ -240,6 +251,7 @@ DEPENDENCIES mysql2 (~> 0.3.20) pry-byebug pry-rails + rack-livereload rails (~> 4.2.4) responders (~> 2.1.0) rubocop diff --git a/Guardfile b/Guardfile index 6145dcb..ef1f7f4 100644 --- a/Guardfile +++ b/Guardfile @@ -27,3 +27,15 @@ guard :minitest do watch(%r{^app/(.*/)?([^/]+)\.rb$}) { |m| "test/#{m[1]}#{m[2]}_test.rb" } watch(%r{^lib/(.*/)?([^/]+)\.rb$}) { |m| "test/lib/#{m[1]}#{m[2]}_test.rb" } end + +guard 'livereload' do + watch(%r{app/views/.+\.(erb|haml|slim)$}) + watch(%r{app/helpers/.+\.rb}) + watch(%r{public/.+\.(css|js|html)}) + watch(%r{config/locales/.+\.yml}) + + # Rails Assets Pipeline + watch(%r{(app|vendor)(/assets/\w+/(.+\.(css|js|html|png|jpg))).*}) do |m| + "/assets/#{m[3]}" + end +end diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index d83690e..4133060 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -2,4 +2,6 @@ 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 + + respond_to :html, :json end diff --git a/app/controllers/children_controller.rb b/app/controllers/children_controller.rb index 1fc50b1..167d85f 100644 --- a/app/controllers/children_controller.rb +++ b/app/controllers/children_controller.rb @@ -1,2 +1,6 @@ class ChildrenController < ApplicationController + def index + @children = Child.all.order(:last_name, :first_name) + respond_with @children + end end diff --git a/app/controllers/staff_controller.rb b/app/controllers/staff_controller.rb index dc0a990..5a7961b 100644 --- a/app/controllers/staff_controller.rb +++ b/app/controllers/staff_controller.rb @@ -1,2 +1,6 @@ class StaffController < ApplicationController + def index + @staff = Person.staff + respond_with @staff + end end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 3e74dea..3334efd 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,2 +1,6 @@ class UsersController < ApplicationController + def index + @users = Person.admins + respond_with @users + end end diff --git a/app/models/child.rb b/app/models/child.rb new file mode 100644 index 0000000..6ad0104 --- /dev/null +++ b/app/models/child.rb @@ -0,0 +1,11 @@ +class Child < ActiveRecord::Base + has_many :parenthoods + has_many :parents, through: :parenthoods, source: :person + + validates :first_name, presence: true + validates :last_name, presence: true + + def name + "#{first_name} #{last_name}" + end +end diff --git a/app/models/parenthood.rb b/app/models/parenthood.rb new file mode 100644 index 0000000..04ff89a --- /dev/null +++ b/app/models/parenthood.rb @@ -0,0 +1,4 @@ +class Parenthood < ActiveRecord::Base + belongs_to :person + belongs_to :child +end diff --git a/app/models/person.rb b/app/models/person.rb new file mode 100644 index 0000000..f51b4aa --- /dev/null +++ b/app/models/person.rb @@ -0,0 +1,25 @@ +class Person < ActiveRecord::Base + has_many :parenthoods + has_many :children, through: :parenthoods + + validates :first_name, presence: true + validates :last_name, presence: true + + scope :with_name, lambda { |name| + where("concat(first_name, ' ', last_name) RLIKE ?", name) + } + + scope :just_parents, lambda { + joins(:children) + .order(:first_name) + .uniq + } + + scope :staff, -> { where(staff: true) } + + scope :admins, -> { where(admin: true) } + + def name + "#{first_name} #{last_name}" + end +end diff --git a/app/views/children/index.haml b/app/views/children/index.haml new file mode 100644 index 0000000..1b47c6e --- /dev/null +++ b/app/views/children/index.haml @@ -0,0 +1,8 @@ +%h1 Children + +- @children.each do |child| + %ul + %li= child.name + %ul + - child.parents.each do |parent| + %li #{parent.name}: #{number_to_phone parent.phone} diff --git a/app/views/parents/index.haml b/app/views/parents/index.haml new file mode 100644 index 0000000..679009f --- /dev/null +++ b/app/views/parents/index.haml @@ -0,0 +1,11 @@ +%h1 Parents + +- @parents.each do |parent| + %ul + %li= parent.name + %li= number_to_phone parent.phone + %li= mail_to parent.email + %li Children: + %ul + - parent.children.each do |child| + %li= child.name diff --git a/app/views/staff/index.haml b/app/views/staff/index.haml new file mode 100644 index 0000000..8f0780b --- /dev/null +++ b/app/views/staff/index.haml @@ -0,0 +1,4 @@ +%h1 Staff +%ul + - @staff.each do |staff| + %li= staff.name diff --git a/app/views/users/index.haml b/app/views/users/index.haml new file mode 100644 index 0000000..551426d --- /dev/null +++ b/app/views/users/index.haml @@ -0,0 +1,5 @@ +%h1 Users + +%ul + - @users.each do |user| + %li= user.name diff --git a/config/database.yml b/config/database.yml index ccc7332..9610906 100644 --- a/config/database.yml +++ b/config/database.yml @@ -1,4 +1,4 @@ -a-- +--- default: &default adapter: mysql2 encoding: utf8 diff --git a/config/environments/development.rb b/config/environments/development.rb index b55e214..4645ff3 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -38,4 +38,7 @@ Rails.application.configure do # Raises error for missing translations # config.action_view.raise_on_missing_translations = true + + # Add Rack::LiveReload to the bottom of the middleware stack with the default options. + config.middleware.insert_after ActionDispatch::Static, Rack::LiveReload end diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 0000000..e3e91f4 --- /dev/null +++ b/db/schema.rb @@ -0,0 +1,50 @@ +# encoding: UTF-8 +# This file is auto-generated from the current state of the database. Instead +# of editing this file, please use the migrations feature of Active Record to +# incrementally modify your database, and then regenerate this schema definition. +# +# Note that this schema.rb definition is the authoritative source for your +# database schema. If you need to create the application database on another +# system, you should be using db:schema:load, not running all the migrations +# from scratch. The latter is a flawed and unsustainable approach (the more migrations +# you'll amass, the slower it'll run and the greater likelihood for issues). +# +# It's strongly recommended that you check this file into your version control system. + +ActiveRecord::Schema.define(version: 20150913220116) do + + create_table "children", force: :cascade do |t| + t.string "first_name", limit: 255 + t.string "last_name", limit: 255 + end + + create_table "pages", force: :cascade do |t| + t.integer "user_id", limit: 4 + t.integer "person_id", limit: 4 + t.string "phone", limit: 255 + t.string "to", limit: 255 + t.string "message", limit: 255 + t.string "status", limit: 255 + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + create_table "parenthoods", force: :cascade do |t| + t.integer "person_id", limit: 4 + t.integer "child_id", limit: 4 + end + + add_index "parenthoods", ["person_id", "child_id"], name: "parentship", using: :btree + + create_table "people", force: :cascade do |t| + t.string "first_name", limit: 255 + t.string "last_name", limit: 255 + t.string "phone", limit: 255 + t.string "email", limit: 255 + t.boolean "admin" + t.boolean "staff" + end + + add_index "people", ["phone"], name: "index_people_on_phone", using: :btree + +end diff --git a/test/controllers/parents_controller_test.rb b/test/controllers/parents_controller_test.rb index 1429c18..1a62f47 100644 --- a/test/controllers/parents_controller_test.rb +++ b/test/controllers/parents_controller_test.rb @@ -2,7 +2,7 @@ require 'test_helper' class ParentsControllerTest < ActionController::TestCase def test_parents - xhr :get, :index + get :index assert response.ok? end end diff --git a/test/lib/secrets_test.rb b/test/lib/secrets_test.rb index 7d593f1..a8398d0 100644 --- a/test/lib/secrets_test.rb +++ b/test/lib/secrets_test.rb @@ -1,7 +1,7 @@ -require File.expand_path '../../test_helper.rb', __FILE__ +require 'test_helper' class SecretsTest < ActiveSupport::TestCase - def test_sanity - assert Secrets - end + # def test_sanity + # assert Secrets + # end end