Skip to content

Commit

Permalink
Merge branch 'feature/rubocop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Calyhre committed May 26, 2017
2 parents 6fb2549 + c03efc3 commit fbf8370
Show file tree
Hide file tree
Showing 33 changed files with 329 additions and 227 deletions.
26 changes: 26 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
AllCops:
TargetRubyVersion: 2.4.1
UseCache: true
Exclude:
- vendor/**/*
- db/schema.rb

Rails:
Enabled: true

Metrics/LineLength:
Max: 100
Metrics/MethodLength:
Max: 20
Metrics/AbcSize:
Max: 20

Style/TrailingCommaInLiteral:
EnforcedStyleForMultiline: comma

Style/FrozenStringLiteralComment:
Enabled: false
Style/Documentation:
Enabled: false
Style/AmbiguousBlockAssociation:
Enabled: false
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ gem 'countries'
gem 'httparty'
gem 'money-rails'
gem 'slim-rails'
# gem 'sass-rails', '~> 5.0'

group :development, :test do
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
gem 'byebug', platforms: %i[mri mingw x64_mingw]
gem 'jazz_fingers'
end

group :development do
gem 'listen', '>= 3.0.5', '< 3.2'
gem 'rubocop', require: false
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
end
15 changes: 15 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ GEM
minitest (~> 5.1)
tzinfo (~> 1.1)
arel (8.0.0)
ast (2.3.0)
awesome_print (1.7.0)
bugsnag (5.3.2)
builder (3.2.3)
Expand Down Expand Up @@ -95,7 +96,10 @@ GEM
nio4r (2.0.0)
nokogiri (1.7.2)
mini_portile2 (~> 2.1.0)
parser (2.4.0.0)
ast (~> 2.2)
pg (0.20.0)
powerpack (0.1.1)
pry (0.10.4)
coderay (~> 1.1.0)
method_source (~> 0.8.1)
Expand Down Expand Up @@ -135,11 +139,20 @@ GEM
method_source
rake (>= 0.8.7)
thor (>= 0.18.1, < 2.0)
rainbow (2.2.2)
rake
rake (12.0.0)
rb-fsevent (0.9.8)
rb-inotify (0.9.8)
ffi (>= 0.5.0)
redis (3.3.3)
rubocop (0.48.1)
parser (>= 2.3.3.1, < 3.0)
powerpack (~> 0.1)
rainbow (>= 1.99.1, < 3.0)
ruby-progressbar (~> 1.7)
unicode-display_width (~> 1.0, >= 1.0.1)
ruby-progressbar (1.8.1)
ruby_dep (1.5.0)
sixarm_ruby_unaccent (1.1.1)
slim (3.0.8)
Expand Down Expand Up @@ -168,6 +181,7 @@ GEM
tilt (2.0.7)
tzinfo (1.2.3)
thread_safe (~> 0.1)
unicode-display_width (1.2.1)
unicode_utils (1.4.0)
websocket-driver (0.6.5)
websocket-extensions (>= 0.1.0)
Expand All @@ -189,6 +203,7 @@ DEPENDENCIES
puma (~> 3.7)
rails (~> 5.1.1)
redis (~> 3.2)
rubocop
slim-rails
spring
spring-watcher-listen (~> 2.0.0)
Expand Down
56 changes: 6 additions & 50 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,61 +1,18 @@
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception

def prices
games = Game.includes(:prices).by_game_code
countries = Price.distinct.pluck(:country).sort
currency = Price.pluck(:currency).include?(params[:currency]) ? params[:currency] : nil

csv = CSV.generate(headers: true) do |rows|
rows << %w(Title).concat(countries)
games.each do |code, games|
game = games.first
prices = games.flat_map(&:prices)
row = []
row << game.title
countries.each do |country|
price = prices.detect { |p| p.country === country }
row << (price ? (currency ? price.value.exchange_to(currency) : price.value) : 'NA')
end
# csv << attributes.map{ |attr| user.send(attr) }
rows << row
end
end

send_data csv, type: 'text/csv; charset=utf-8; header=present'
end

def best_deals
games = Game.includes(:prices).by_game_code
currencies = Price.distinct.pluck(:currency).sort
all_games = Game.includes(:prices).by_game_code
currencies = Price.distinct.pluck(:currency).sort

csv = CSV.generate(headers: true) do |rows|
rows << ['Title', 'Country'].concat(currencies)
rows << %w[Title Country].concat(currencies)

games.each do |code, games|
game = games.first
all_games.each do |_code, games|
lowest = games.flat_map(&:prices).min_by { |price| price.value.exchange_to('USD') }
next if !lowest.present?
next if lowest.blank?
rates = currencies.map { |c| lowest.value.exchange_to(c) }
rows << [game.title, ISO3166::Country[lowest.country].translation('en')].concat(rates)
end
end

send_data csv, type: 'text/csv; charset=utf-8; header=present'
end

def rates
currencies = Price.distinct.pluck(:currency).sort

csv = CSV.generate(headers: true) do |rows|
rows << [''].concat(currencies)

currencies.each do |from|
row = [from]
currencies.each do |to|
row << Money.default_bank.get_rate(from, to)
end
rows << row
rows << [game.first.title, ISO3166::Country[lowest.country].translation('en')].concat(rates)
end
end

Expand Down Expand Up @@ -83,7 +40,6 @@ def glossary
currency = ISO3166::Country.find_country_by_currency(code).currency
rows << [code, currency.name]
end

end

send_data csv, type: 'text/csv; charset=utf-8; header=present'
Expand Down
31 changes: 31 additions & 0 deletions app/controllers/prices_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class PricesController < ApplicationController
def index
@current_page = 'prices'
@games = Game.includes(:prices).by_game_code
@countries = Price.distinct.pluck(:country).sort
@currencies = Price.distinct.pluck(:currency).sort
@currency = @currencies.include?(params[:currency]) ? params[:currency] : nil

respond_to do |format|
format.html
format.csv { send_data(render_csv, type: 'text/csv; charset=utf-8; header=present') }
end
end

private

def render_csv
CSV.generate(headers: true) do |csv|
csv << %w[Title].concat(@countries)

@games.each do |_code, games|
prices = games.flat_map(&:prices)
csv << [games.first.title] + @countries.map do |country|
price = prices.detect { |p| p.country == country }
next 'NA' unless price
@currency ? price.value.exchange_to(@currency) : price.value
end
end
end
end
end
21 changes: 12 additions & 9 deletions app/models/game.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class Game < ApplicationRecord
REGIONS = %w(europe americas asia).freeze
GAME_CODE_FORMAT = /\A[0-9A-Z]{4}\z/.freeze
NSUID_CODE_FORMAT = /\A7001000000[0-9]{4}\z/.freeze
REGIONS = %w[europe americas asia].freeze
GAME_CODE_FORMAT = /\A[0-9A-Z]{4}\z/
NSUID_CODE_FORMAT = /\A7001000000[0-9]{4}\z/

attribute :game_code, :string
attribute :region, :string
Expand All @@ -17,11 +17,9 @@ class Game < ApplicationRecord

has_many :prices

scope :order_by_title, -> {
order('LOWER(title COLLATE "C")')
}
scope :order_by_title, -> { order('LOWER(title COLLATE "C")') }

scope :order_by_region, -> {
scope :order_by_region, lambda {
order_by = ['case']
REGIONS.each_with_index do |region, index|
order_by << "WHEN games.region='#{region}' THEN #{index}"
Expand All @@ -30,6 +28,11 @@ class Game < ApplicationRecord
order(order_by.join(' '))
}

scope :by_game_code, -> { order_by_title.group_by(&:game_code).each { |_,games| games.sort_by! { |game| REGIONS.index(game.region) } } }
scope :by_region, -> { order_by_title.order_by_region.group_by(&:region) }
scope :by_game_code, lambda {
order_by_title.group_by(&:game_code).each do |_, games|
games.sort_by! { |game| REGIONS.index(game.region) }
end
}

scope :by_region, -> { order_by_title.order_by_region.group_by(&:region) }
end
2 changes: 1 addition & 1 deletion app/views/games/index.slim
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ table.table.table-responsive
= cover ? image_tag(cover, style: 'max-width:120px;max-height:120px;', class: 'rounded mx-auto d-block', title: 'wat') : nil
td.game--code rowspan=games.count = game.game_code
td.game--region = game.region
td.game--title.text-overflow style="width:100%;max-width:0" = game.title
td.game--title.text-overflow style="width:100%;max-width:0" title=games.first.title = game.title
td.game--nsuid = game.nsuid || 'NA'
td.game--release-date
time.text-overflow datetime=game.release_date.iso8601 = game.release_date.strftime("%b #{game.release_date.day.ordinalize}, %Y")
11 changes: 8 additions & 3 deletions app/views/layouts/application.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ html
overflow: hidden;
text-overflow: ellipsis;
}
.text-muted {
opacity: .2;
}

body
nav.navbar.navbar-toggleable-sm.navbar-light.bg-faded.sticky-top style="width: 100%"
Expand All @@ -28,8 +31,10 @@ html
#navbarSupportedContent.navbar-collapse.collapse
ul.navbar-nav.mr-auto
li.nav-item class=(@current_page == 'games' ? 'active' : nil)
= link_to 'Game list', '#', class: 'nav-link'
li.nav-item
= link_to 'All prices', '#', class: 'nav-link disabled'
= link_to 'Game list', games_url, class: 'nav-link'
li.nav-item class=(@current_page == 'prices' ? 'active' : nil)
= link_to 'All prices', prices_url, class: 'nav-link'

= content_for :navbar_right

= yield
1 change: 0 additions & 1 deletion bin/setup
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ chdir APP_ROOT do
# Install JavaScript dependencies if using Yarn
# system('bin/yarn')


# puts "\n== Copying sample files =="
# unless File.exist?('config/database.yml')
# cp 'config/database.yml.sample', 'config/database.yml'
Expand Down
6 changes: 3 additions & 3 deletions bin/yarn
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
VENDOR_PATH = File.expand_path('..', __dir__)
Dir.chdir(VENDOR_PATH) do
begin
exec "yarnpkg #{ARGV.join(" ")}"
exec "yarnpkg #{ARGV.join(' ')}"
rescue Errno::ENOENT
$stderr.puts "Yarn executable was not detected in the system."
$stderr.puts "Download Yarn at https://yarnpkg.com/en/docs/install"
$stderr.puts 'Yarn executable was not detected in the system.'
$stderr.puts 'Download Yarn at https://yarnpkg.com/en/docs/install'
exit 1
end
end
4 changes: 2 additions & 2 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
config.consider_all_requests_local = true

# Enable/disable caching. By default caching is disabled.
if Rails.root.join('tmp/caching-dev.txt').exist?
if Rails.root.join('tmp', 'caching-dev.txt').exist?
config.action_controller.perform_caching = true

config.cache_store = :memory_store
config.public_file_server.headers = {
'Cache-Control' => "public, max-age=#{2.days.seconds.to_i}"
'Cache-Control' => "public, max-age=#{2.days.seconds.to_i}",
}
else
config.action_controller.perform_caching = false
Expand Down
7 changes: 4 additions & 3 deletions config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
# Do not fallback to assets pipeline if a precompiled asset is missed.
config.assets.compile = false

# `config.assets.precompile` and `config.assets.version` have moved to config/initializers/assets.rb
# `config.assets.precompile` and `config.assets.version` have moved to
# config/initializers/assets.rb

# Enable serving of images, stylesheets, and JavaScripts from an asset server.
# config.action_controller.asset_host = 'http://assets.example.com'
Expand All @@ -52,7 +53,7 @@
config.log_level = :debug

# Prepend all log lines with the following tags.
config.log_tags = [ :request_id ]
config.log_tags = [:request_id]

# Use a different cache store in production.
# config.cache_store = :mem_cache_store
Expand Down Expand Up @@ -80,7 +81,7 @@
# require 'syslog/logger'
# config.logger = ActiveSupport::TaggedLogging.new(Syslog::Logger.new 'app-name')

if ENV["RAILS_LOG_TO_STDOUT"].present?
if ENV['RAILS_LOG_TO_STDOUT'].present?
logger = ActiveSupport::Logger.new(STDOUT)
logger.formatter = config.log_formatter
config.logger = ActiveSupport::TaggedLogging.new(logger)
Expand Down
2 changes: 1 addition & 1 deletion config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# Configure public file server for tests with Cache-Control for performance.
config.public_file_server.enabled = true
config.public_file_server.headers = {
'Cache-Control' => "public, max-age=#{1.hour.seconds.to_i}"
'Cache-Control' => "public, max-age=#{1.hour.seconds.to_i}",
}

# Show full error reports and disable caching.
Expand Down
6 changes: 4 additions & 2 deletions config/initializers/backtrace_silencers.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# Be sure to restart your server when you modify this file.

# You can add backtrace silencers for libraries that you're using but don't wish to see in your backtraces.
# You can add backtrace silencers for libraries that you're using but don't wish to see in your
# backtraces.
# Rails.backtrace_cleaner.add_silencer { |line| line =~ /my_noisy_library/ }

# You can also remove all the silencers if you're trying to debug a problem that might stem from framework code.
# You can also remove all the silencers if you're trying to debug a problem that might stem from
# framework code.
# Rails.backtrace_cleaner.remove_silencers!
2 changes: 1 addition & 1 deletion config/initializers/bugsnag.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Bugsnag.configure do |config|
config.api_key = ENV.fetch('BUGSNAG_API_KEY', '')
config.api_key = ENV.fetch('BUGSNAG_API_KEY') { '' }
end
2 changes: 1 addition & 1 deletion config/initializers/money.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
MoneyRails.configure do |config|
REDIS_URL = ENV.fetch('REDIS_URL', 'redis://localhost:6379')
REDIS_URL = ENV.fetch('REDIS_URL') { 'redis://localhost:6379' }
config.default_bank = Money::Bank::VariableExchange.new(ExchangeRate.new(url: REDIS_URL))
end
6 changes: 3 additions & 3 deletions config/puma.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
# the maximum value specified for Puma. Default is set to 5 threads for minimum
# and maximum; this matches the default thread size of Active Record.
#
threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 }
threads_count = ENV.fetch('RAILS_MAX_THREADS') { 5 }
threads threads_count, threads_count

# Specifies the `port` that Puma will listen on to receive requests; default is 3000.
#
port ENV.fetch("PORT") { 3000 }
port ENV.fetch('PORT') { 3000 }

# Specifies the `environment` that Puma will run in.
#
environment ENV.fetch("RAILS_ENV") { "development" }
environment ENV.fetch('RAILS_ENV') { 'development' }

# Specifies the number of `workers` to boot in clustered mode.
# Workers are forked webserver processes. If using threads and workers together
Expand Down
Loading

0 comments on commit fbf8370

Please sign in to comment.