Skip to content

Commit

Permalink
Manual rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
alexvkcr authored and katafira committed Sep 1, 2016
1 parent b7139c5 commit 653e596
Show file tree
Hide file tree
Showing 16 changed files with 192 additions and 112 deletions.
20 changes: 5 additions & 15 deletions app/controllers/admin_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
# They have an speicific layout to reflec this
class AdminController < ApplicationController
layout 'admin'
before_action :admin_user?
skip_before_action :authenticate_user!
before_action :authenticate_admin!

def dashboard
@users = User.all
Expand All @@ -19,19 +20,8 @@ def rooms
@rooms = Room.all
end

def status_lock_in_changes

end

def switch_lock_in_changes

end

private

def admin_user?
unless current_user.admin?
redirect_to home_path
end
def switch_lock_admin
ApplicationHelper.switch_lock
render nothing: true
end
end
1 change: 0 additions & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ class ApplicationController < ActionController::Base
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
before_action :configure_permitted_parameters, if: :devise_controller?
before_action :authenticate_user!

# Protection is in routes

Expand Down
14 changes: 12 additions & 2 deletions app/controllers/assignments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ def new
end

def create
Offspring.transaction do
if admin_allows_changes?
Offspring.transaction do
of = Offspring.lock.find_by_id(params["format"])
of.shift = Shift.find_by_id(params["shift"])
of.save
redirect_to root_url
end
else
flash[:alert] = "No se puedo añadir al niño en este momento, espere."
end
redirect_to root_url
end

def destroy
Expand All @@ -19,4 +23,10 @@ def destroy
of.save
redirect_to new_assignment_path(of)
end

private

def admin_allows_changes?
!ApplicationHelper.status_lock?
end
end
7 changes: 4 additions & 3 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Offsprings are dependent on them
# They are the ones that need to perform the process on behalf of the offspring
class UsersController < ApplicationController
before_action :authenticate_user!
def show
@user = User.find_by_id(params[:id]) || current_user
end
Expand All @@ -25,12 +26,12 @@ def destroy
private

def delete_possible?(user)
if user.nil?
if admin_signed_in?
return true
elsif user.nil?
return false
elsif user == current_user
return false
elsif !current_user.admin?
return false
else
return true
end
Expand Down
8 changes: 8 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,12 @@ def week_day_convert(day)
week = {1 => 'Lunes', 2 => 'Martes', 3 => 'Miércoles', 4 => 'Jueves', 5 => 'Viernes', 6 => 'Sábado', 7 => 'Domingo'}
week[day] || 'Ninguno'
end

def self.status_lock?
return Rails.application.config.lock.status
end

def self.switch_lock
Rails.application.config.lock.status = ! Rails.application.config.lock.status
end
end
7 changes: 7 additions & 0 deletions config/initializers/lock.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

# lock_config = Rails.application.config_for(:lock)

Rails.application.configure do
config.lock = ActiveSupport::OrderedOptions.new
config.lock.status = false
end
3 changes: 3 additions & 0 deletions config/locales/es.yml
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,6 @@ es:
first_name: "Nombre"
last_name: "Apellidos"
grade: "Curso"
controllers:
assignment:
admin_locked_create: "No se puedo añadir al niño en este momento, espere."
3 changes: 2 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
devise_for :admins
# when admin is authenticated
authenticate :admins do
root to: "static_pages#home"
get 'home' => 'static_pages#home'
get 'admin' => 'admin#dashboard'
get 'offsprings' => 'admin#offsprings'
get 'rooms' => 'admin#rooms'
post 'switch_lock_admin' => 'admin#switch_lock_admin'
end
# Instructions for the apo
get 'static_pages/intructions'
Expand Down
6 changes: 6 additions & 0 deletions db/migrate/20160817083327_add_first_name_to_admins.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddFirstNameToAdmins < ActiveRecord::Migration
def change
add_column :admins, :first_name, :string
add_column :admins, :last_name, :string
end
end
14 changes: 6 additions & 8 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20160728082715) do
ActiveRecord::Schema.define(version: 20160817083327) do

create_table "admins", force: true do |t|
t.string "email", default: "", null: false
Expand All @@ -28,7 +28,6 @@
t.datetime "updated_at", null: false
t.string "first_name"
t.string "last_name"
t.string "phone"
end

add_index "admins", ["email"], name: "index_admins_on_email", unique: true
Expand Down Expand Up @@ -67,22 +66,21 @@
add_index "shifts", ["room_id"], name: "index_shifts_on_room_id"

create_table "users", force: true do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "first_name"
t.string "last_name"
t.string "phone"
t.boolean "admin", default: false
t.string "confirmation_token"
t.datetime "confirmed_at"
t.datetime "confirmation_sent_at"
Expand Down
6 changes: 2 additions & 4 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@

1.times do
a = FactoryGirl.build(:admin)
a.name = "Alberto"
a.first_name = "Alberto"
a.last_name = "Ramos"
a.email = "[email protected]"
a.save
2.times do
a.offsprings << FactoryGirl.build(:offspring, user: a, last_name: "Unamuno")
end
end

99.times do
Expand Down
86 changes: 68 additions & 18 deletions spec/controllers/admin_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
require 'rails_helper'

RSpec.describe AdminController, type: :controller do
let(:user_admin) { FactoryGirl.create(:user, :administrator) }

let(:user_admin) { FactoryGirl.create(:admin) }
context "admin user" do
describe "GET #dashboard" do
it "returns http success" do
Expand All @@ -10,32 +11,81 @@
expect(response).to have_http_status(:success)
end
end
describe "can block any change in the data" do
before(:all)do
expect(user_admin).to be_an_instance_of(Admin)
describe "#POST switch_lock_admin" do
before(:each)do # we want to make sure we are starting in switch lock with false value
sign_in user_admin
post :switch_lock_admin if ApplicationHelper.status_lock?
end
before(:each)do
get :status_lock_in_changes
if @status == true
post :switch_lock_in_changes
end
it "locks and unlocks changes succesfully" do
post :switch_lock_admin
expect(ApplicationHelper.status_lock?).to be true
post :switch_lock_admin
expect(ApplicationHelper.status_lock?).to be false
end
it "succesfully" do #starts in false switch lock
post :switch_lock_in_changes
describe ",when enabled," do
before(:each)do
post :switch_lock_admin unless ApplicationHelper.status_lock?
end
before(:all)do
user = FactoryGirl.build(:user)
user.save
end

it "users cannot add offspring" do
ref = @controller # Storing pointer to current controller
@controller = OffspringsController.new # Setting User controller to call
sign_in user # Start user session
expect do
post :create, offspring: {first_name: "pepe", last_name: "kata", grade: :primary_first}
end.to change(user.offsprings, :count).by(0)
expect(response).to redirect_to(static_pages_intructions_path)
@controller = ref # Restoring previous admin controller
end

it "users cannot delete offspring"do
off = FactoryGirl.build(:offspring, user: user)
off.save
ref = @controller # Storing pointer to current controller
@controller = OffspringsController.new # Setting User controller to call
sign_in user # Start user session
expect do
delete :destroy, id: off.id
end.to change(user.offsprings, :count).by(0)
@controller = ref # Restoring previous admin controller
end

it "users cannot assign offspring"do
off = FactoryGirl.build(:offspring, user: user)
off.save
shi = FactoryGirl.build(:shift)
shi.save
ref = @controller # Storing pointer to current controller
@controller = AssignmentsController.new # Setting User controller to call
sign_in user # Start user session
expect do
post :create, format: off.id, shift: shi.id
end.to change(shi.offsprings, :count).by(0)
@controller = ref # Restoring previous admin controller
end

it "it still allows users to delete their own account" do
expect(ApplicationHelper.status_lock?).to be true
ref = @controller # Storing pointer to current controller
@controller = UsersController.new # Setting User controller to call
sign_in user # Start user session
expect do
delete :destroy, id: user.id
end.to change{User.count}.by(-1)
@controller = ref # Restoring previous admin controller
end
end
pending "and unblock changes"
pending ", users cas still delete their own account"
end
end

let(:user) { FactoryGirl.create(:user) }
context "non-admin user" do
describe "GET #dashboard" do
it "returns http error" do
sign_in user
get :dashboard
expect(response).to redirect_to(home_path)
end
pending "returns http error"
end
end
end
7 changes: 6 additions & 1 deletion spec/factories/admins.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
FactoryGirl.define do
factory :admin do

first_name { Faker::Name.first_name }
last_name { Faker::Name.last_name }
email { Faker::Internet.safe_email }

password "foobar123"
password_confirmation "foobar123"
end
end
5 changes: 0 additions & 5 deletions spec/factories/users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,5 @@

confirmed_at Time.current

# FactoryGirl.create(user, :administrator)

trait :administrator do
admin true
end
end
end
12 changes: 11 additions & 1 deletion spec/helpers/application_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require 'rails_helper'

# Specs in this file have access to a helper object that includes
# the ApplicationHelper. For example:
#
Expand All @@ -20,4 +19,15 @@
it "returns complex title when page_title with spaces is given" do
expect(helper.full_title("Datos de Contacto")).to eq("Datos de Contacto | Catequesis")
end
it "returns correctly the status in lock" do
expect(ApplicationHelper.status_lock?).to be(false)
end
it "switches status in lock correctly in both directions" do
old_status = ApplicationHelper.status_lock? # Status A
ApplicationHelper.switch_lock # Change to status B
expect(old_status).not_to be(ApplicationHelper.status_lock?) # Status A and B are different
old_status = ApplicationHelper.status_lock? # Status B
ApplicationHelper.switch_lock # Change to status A
expect(old_status).not_to be(ApplicationHelper.status_lock?) # Status B and A are different
end
end
Loading

0 comments on commit 653e596

Please sign in to comment.