Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor optimizations #13

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .ruby-version

This file was deleted.

19 changes: 10 additions & 9 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
source 'https://rubygems.org'
source 'https://rails-assets.org'
ruby '2.1.2'
ruby '2.2.2'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.1.6'
gem 'rails', '4.2.2'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.3'
# Use Uglifier as compressor for JavaScript assets
Expand All @@ -20,10 +20,10 @@ gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc
gem 'sdoc', '~> 0.4.0', group: :doc

# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring', group: :development
gem 'spring', group: :development

# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
Expand All @@ -37,8 +37,8 @@ gem 'spring', group: :development
# Use debugger
# gem 'debugger', group: [:development, :test]

gem "autoprefixer-rails"
gem 'rails-assets-angular', "1.3.0.rc.4"
gem 'autoprefixer-rails'
gem 'rails-assets-angular', '1.3.0.rc.4'
gem 'pusher'

group :production, :staging do
Expand All @@ -47,9 +47,10 @@ group :production, :staging do
end

group :development do
gem "better_errors"
gem "binding_of_caller"
gem 'rubocop'
gem 'better_errors'
gem 'binding_of_caller'
gem 'mysql2'
end

gem 'dotenv-rails', :groups => [:development, :test]
gem 'dotenv-rails', groups: [:development, :test]
7 changes: 3 additions & 4 deletions app/controllers/admin_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ def dashboard
private

def authenticate
if Rails.env.production?
authenticate_or_request_with_http_basic do |username, password|
username == ENV['ADMIN_USERNAME'] && password == ENV['ADMIN_PASSWORD']
end
return unless Rails.env.production?
authenticate_or_request_with_http_basic do |username, password|
username == ENV['ADMIN_USERNAME'] && password == ENV['ADMIN_PASSWORD']
end
end
end
27 changes: 10 additions & 17 deletions app/controllers/questions_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
class QuestionsController < ApplicationController

before_filter :set_question, only: [:show, :results]
before_filter :check_secret_is_unique, only: [:create]

Expand All @@ -14,47 +13,41 @@ def create
@question.save!

params[:options].each do |option|
if option[:title] != ""
new_option = Option.new
new_option.title = option[:title]
new_option.question_id = @question.id
new_option.save!
end
next unless option[:title] != ''
new_option = Option.new(title: option[:title], question_id: @question.id)
new_option.save!
end

redirect_to "/#{@question.secret}"
end

def show
vote_id = cookies["vote_#{@question.secret}"]
@vote = Vote.where({secret: vote_id}).first_or_initialize
@vote = Vote.where(secret: vote_id).first_or_initialize
end

def results
@options = @question.options
end

def check_secret_availability
render json: { available: !Question.where({secret: params[:secret]}).exists? }
render json: { available: !Question.where(secret: params[:secret]).exists? }
end

private

def set_question
@question = Question.where({secret: params[:secret]}).first
@question = Question.find_by_secret!(params[:secret])
end

def question_params
params.require(:question).permit(:title, :secret)
end

def check_secret_is_unique
if defined? params[:question][:secret]
if Question.where({secret: params[:question][:secret]}).exists?
@question = Question.new(question_params)
redirect_to :back, notice: 'Sorry that URL is taken'
end
end
return unless defined? params[:question][:secret]
return unless Question.where(secret: params[:question][:secret]).exists?
@question = Question.new(question_params)
redirect_to :back, notice: 'Sorry that URL is taken'
end

end
20 changes: 10 additions & 10 deletions app/controllers/votes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@ class VotesController < ApplicationController
def update
question_id = params[:vote][:question_id]
vote_id = cookies["vote_#{question_id}"]
vote = Vote.where({secret: vote_id}).first_or_initialize
vote = Vote.where(secret: vote_id).first_or_initialize

question = Question.where({secret: question_id}).first
question = Question.find_by_secret!(question_id)
vote.secret = SecureRandom.urlsafe_base64(nil, false) unless vote.secret
vote.question_id = question.id unless vote.question_id
vote.option_id = Option.find(params[:vote][:option_id]).id

Pusher[question_id].trigger("vote", {})
Pusher[question_id].trigger('vote', {})

if vote.save!
cookies.permanent["vote_#{question.secret}"] = vote.secret
return unless vote.save!

respond_to do |format|
format.html { redirect_to "/#{question.secret}" }
format.json { render json: {}, status: :created }
end
cookies.permanent["vote_#{question.secret}"] = vote.secret

respond_to do |format|
format.html { redirect_to "/#{question.secret}" }
format.json { render json: {}, status: :created }
end
end

Expand All @@ -29,7 +29,7 @@ def show
private

def set_question
@question = Question.where({secret: params[:secret]}).first
@question = Question.where(secret: params[:secret]).first
end

def question_params
Expand Down
2 changes: 1 addition & 1 deletion app/models/option.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ class Option < ActiveRecord::Base
belongs_to :question

def votes
Vote.where({ option_id: self.id }).count
Vote.where(option_id: id).count
end
end
4 changes: 2 additions & 2 deletions bin/rails
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/usr/bin/env ruby
begin
load File.expand_path("../spring", __FILE__)
load File.expand_path('../spring', __FILE__)
rescue LoadError
end
APP_PATH = File.expand_path('../../config/application', __FILE__)
APP_PATH = File.expand_path('../../config/application', __FILE__)
require_relative '../config/boot'
require 'rails/commands'
2 changes: 1 addition & 1 deletion bin/rake
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env ruby
begin
load File.expand_path("../spring", __FILE__)
load File.expand_path('../spring', __FILE__)
rescue LoadError
end
require_relative '../config/boot'
Expand Down
12 changes: 6 additions & 6 deletions bin/spring
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
# It gets overwritten when you run the `spring binstub` command

unless defined?(Spring)
require "rubygems"
require "bundler"
require 'rubygems'
require 'bundler'

if match = Bundler.default_lockfile.read.match(/^GEM$.*?^ spring \((.*?)\)$.*?^$/m)
ENV["GEM_PATH"] = ([Bundler.bundle_path.to_s] + Gem.path).join(File::PATH_SEPARATOR)
ENV["GEM_HOME"] = ""
ENV['GEM_PATH'] = ([Bundler.bundle_path.to_s] + Gem.path).join(File::PATH_SEPARATOR)
ENV['GEM_HOME'] = ''
Gem.paths = ENV

gem "spring", match[1]
require "spring/binstub"
gem 'spring', match[1]
require 'spring/binstub'
end
end
2 changes: 1 addition & 1 deletion config.ru
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# This file is used by Rack-based servers to start the application.

require ::File.expand_path('../config/environment', __FILE__)
require ::File.expand_path('../config/environment', __FILE__)
run Rails.application
2 changes: 1 addition & 1 deletion config/initializers/cookies_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Be sure to restart your server when you modify this file.

Rails.application.config.action_dispatch.cookies_serializer = :json
Rails.application.config.action_dispatch.cookies_serializer = :json
1 change: 0 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
Rails.application.routes.draw do

resources :questions
get '/admin' => 'admin#dashboard'
put '/votes' => 'votes#update'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class AddIndicesForBetterPerformance < ActiveRecord::Migration
def change
add_index :questions, :secret
add_index :votes, :question_id
add_index :votes, :option_id
end
end
16 changes: 12 additions & 4 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,36 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20141004134727) do
ActiveRecord::Schema.define(version: 20151014184947) do

create_table "options", force: true do |t|
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

create_table "options", force: :cascade do |t|
t.string "title"
t.integer "question_id"
t.datetime "created_at"
t.datetime "updated_at"
end

create_table "questions", force: true do |t|
create_table "questions", force: :cascade do |t|
t.string "title"
t.string "secret"
t.datetime "created_at"
t.datetime "updated_at"
end

create_table "votes", force: true do |t|
add_index "questions", ["secret"], name: "index_questions_on_secret", using: :btree

create_table "votes", force: :cascade do |t|
t.string "secret"
t.integer "question_id"
t.integer "option_id"
t.datetime "created_at"
t.datetime "updated_at"
end

add_index "votes", ["option_id"], name: "index_votes_on_option_id", using: :btree
add_index "votes", ["question_id"], name: "index_votes_on_question_id", using: :btree

end