Skip to content

Commit

Permalink
boots into console with scopes fixed and included core-extension modu…
Browse files Browse the repository at this point in the history
…les cleanly namespaced
  • Loading branch information
armandofox committed Jun 14, 2017
1 parent ec047fe commit 524e046
Show file tree
Hide file tree
Showing 24 changed files with 290 additions and 272 deletions.
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ gem 'json'
gem 'mechanize'
gem 'mysql'
gem 'nokogiri'
gem 'protected_attributes' # remove once we migrate to Strong Parameters
gem 'rails', '4.2.6' # 4
gem 'rake'
gem 'ruport'
Expand Down Expand Up @@ -65,6 +66,8 @@ group :development, :test do
gem 'sdoc', '~> 0.4.0'
gem 'rspec-rails'
gem 'simplecov'
gem 'spring'
gem 'sqlite3'
gem 'timecop'
gem 'web-console', '~> 2.0'
end
5 changes: 5 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ GEM
temple (>= 0.8.0)
tilt
hashery (2.1.2)
hominid (3.0.5)
http-cookie (1.0.3)
domain_name (~> 0.5)
i18n (0.8.4)
Expand Down Expand Up @@ -183,6 +184,8 @@ GEM
prawn (0.12.0)
pdf-reader (>= 0.9.0)
ttfunk (~> 1.0.2)
protected_attributes (1.1.3)
activemodel (>= 4.0.1, < 5.0)
public_suffix (2.0.5)
rack (1.6.8)
rack-test (0.6.3)
Expand Down Expand Up @@ -327,6 +330,7 @@ DEPENDENCIES
faye-websocket
figaro
haml
hominid
i18n
jbuilder (~> 2.0)
jquery-rails
Expand All @@ -338,6 +342,7 @@ DEPENDENCIES
mysql
nokogiri
poltergeist
protected_attributes
rack-test
rails (= 4.2.6)
rake
Expand Down
1 change: 0 additions & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ class ApplicationController < ActionController::Base

require 'csv.rb'
require 'string_extras.rb'
require 'date_time_extras.rb'

# Session keys
# :cid id of logged in user; if absent, nobody logged in
Expand Down
13 changes: 0 additions & 13 deletions app/helpers/javascript_tag_helper.rb

This file was deleted.

3 changes: 0 additions & 3 deletions app/models/auto_importer.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
require 'date_time_extras'
require 'string_extras'

class AutoImporter

attr_accessor :import, :messages, :email
Expand Down
1 change: 0 additions & 1 deletion app/models/customer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ class Customer < ActiveRecord::Base
require_dependency 'customer/scopes'
require_dependency 'customer/birthday'
require_dependency 'customer/merge'
require_dependency '../lib/date_time_extras'

include Authentication
include Authentication::ByPassword
Expand Down
66 changes: 28 additions & 38 deletions app/models/customer/scopes.rb
Original file line number Diff line number Diff line change
@@ -1,51 +1,41 @@
class Customer < ActiveRecord::Base
default_scope :order => 'last_name, zip'
default_scope { order('last_name, zip') }

scope :subscriber_during, ->(seasons) {
joins(:vouchertypes).where('vouchertypes.subscription = ? AND vouchertypes.season IN (?)', true, seasons)
}

named_scope :subscriber_during, lambda { |seasons|
{ :joins => :vouchertypes,
:conditions => ['vouchertypes.subscription = ? AND vouchertypes.season IN (?)',
true, seasons] }}


named_scope :purchased_any_vouchertypes, lambda { |vouchertype_ids|
{ :joins => :vouchertypes,
:conditions => ['vouchertypes.id IN (?)', vouchertype_ids],
:select => 'DISTINCT customers.*'}}
scope :purchased_any_vouchertypes, ->(vouchertype_ids) {
joins(:vouchertypes).where('vouchertypes.id IN (?)', vouchertype_ids).select('distinct customers.*')
}

def self.purchased_no_vouchertypes(vouchertype_ids)
Customer.all - Customer.purchased_any_vouchertypes(vouchertype_ids)
end


named_scope :seen_any_of, lambda { |show_ids|
{ :joins => [:vouchers,:showdates],
:conditions => ['items.customer_id = customers.id AND
items.showdate_id = showdates.id AND
items.type = "Voucher" AND
showdates.show_id IN (?)', show_ids],
:select => 'DISTINCT customers.*'
}}
scope :seen_any_of, ->(show_ids) {
joins(:vouchers, :showdates).
where('items.customer_id = customers.id AND items.showdate_id = showdates.id AND
items.type = "Voucher" AND showdates.show_id IN (?)', show_ids).
select('DISTINCT customers.*')
}

def self.seen_none_of(show_ids) ; Customer.all - Customer.seen_any_of(show_ids) ; end

named_scope :with_open_subscriber_vouchers, lambda { |vtypes|
{ :joins => ',items',
:conditions => ['items.customer_id = customers.id AND
items.type = "Voucher" AND
scope :with_open_subscriber_vouchers, ->(vtypes) {
joins(:items).
where('items.customer_id = customers.id AND items.type = "Voucher" AND
(items.showdate_id = 0 OR items.showdate_id IS NULL) AND
items.vouchertype_id IN (?)', vtypes],
:select => 'DISTINCT customers.*'
}}


named_scope :donated_during, lambda { |start_date, end_date, amount|
{ :joins => ',items,orders',
:select => 'DISTINCT customers.*',
:conditions => ['items.customer_id = customers.id AND
items.amount >= ? AND
items.type = "Donation" AND
orders.sold_on BETWEEN ? AND ?',
amount, start_date, end_date] }}

items.vouchertype_id IN (?)', vtypes).
select('DISTINCT customers.*')
}

scope :donated_during, ->(start_date, end_date, amount) {
joins(:items, :orders).
where(%q{items.customer_id = customers.id AND items.amount >= ? AND items.type = "Donation"
AND orders.sold_on BETWEEN ? AND ?},
amount, start_date, end_date).
select('distinct customers.*')
}

end
21 changes: 10 additions & 11 deletions app/models/show.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,11 @@ def self.current_or_next
Showdate.current_or_next.try(:show)
end

named_scope :current_and_future, lambda {
{:joins => :showdates,
:select => 'DISTINCT shows.*',
:conditions => ['showdates.thedate >= ?', 1.day.ago],
:order => 'opening_date ASC'
}
scope :current_and_future, -> {
joins(:showdates).
where('showdates.thedate >= ?', 1.day.ago).
select('DISTINCT shows.*').
order('opening_date ASC')
}

def has_showdates? ; !showdates.empty? ; end
Expand All @@ -60,9 +59,9 @@ def self.all_for_season(season=Time.this_season)
:include => :showdates)
end

named_scope :all_for_seasons, lambda { |from,to|
{:conditions => ['opening_date BETWEEN ? AND ?',
Time.at_beginning_of_season(from), Time.at_end_of_season(to)] }
scope :all_for_seasons, ->(from,to) {
where('opening_date BETWEEN ? AND ?',
Time.at_beginning_of_season(from), Time.at_end_of_season(to))
}

def self.seasons_range
Expand All @@ -77,8 +76,8 @@ def self.type(arg)
TYPES.include?(arg) ? arg : REGULAR_SHOW
end

named_scope :of_type, lambda { |type|
{:conditions => ["event_type = ?", self.type(type) ] }
scope :of_type, ->(type) {
where('event_type = ?', self.type(type))
}

def season
Expand Down
8 changes: 3 additions & 5 deletions app/views/layouts/application.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,14 @@
= stylesheet_link_tag 'admin', :media => 'all'
= stylesheet_link_tag 'venue/default', :media => 'all'

-# Rails3: no need to redefine default JS files in javascript_tag_helper.rb
= javascript_include_tag_for_jquery :all
= javascript_include_tag 'application'
%script{:type => 'text/javascript', :src => 'https://js.stripe.com/v1'}

%title= @page_title || h(controller.action_name.humanize)
%meta{'http-equiv'=>'content-type', :content=>'text/html; charset=utf-8'}/
%meta{:name=>:description, :content=>''}/
-# Rails3 provides the following 2 lines via csrf_meta_tag helper
%meta{:name => 'csrf-param', :content => Rack::Utils.escape_html(request_forgery_protection_token)}
%meta{:name => 'csrf-token', :content => Rack::Utils.escape_html(form_authenticity_token)}

= csrf_meta_tags

%body{:id=>"#{controller.controller_name}_#{controller.action_name}", :class=>(Figaro.env.sandbox ? 'testing.default' : 'default')}

Expand Down
3 changes: 3 additions & 0 deletions bin/bundle
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env ruby
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
load Gem.bin_path('bundler', 'bundle')
9 changes: 9 additions & 0 deletions bin/rails
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env ruby
begin
load File.expand_path('../spring', __FILE__)
rescue LoadError => e
raise unless e.message.include?('spring')
end
APP_PATH = File.expand_path('../../config/application', __FILE__)
require_relative '../config/boot'
require 'rails/commands'
9 changes: 9 additions & 0 deletions bin/rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env ruby
begin
load File.expand_path('../spring', __FILE__)
rescue LoadError => e
raise unless e.message.include?('spring')
end
require_relative '../config/boot'
require 'rake'
Rake.application.run
29 changes: 29 additions & 0 deletions bin/setup
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env ruby
require 'pathname'

# path to your application root.
APP_ROOT = Pathname.new File.expand_path('../../', __FILE__)

Dir.chdir APP_ROOT do
# This script is a starting point to setup your application.
# Add necessary setup steps to this file:

puts "== Installing dependencies =="
system "gem install bundler --conservative"
system "bundle check || bundle install"

# puts "\n== Copying sample files =="
# unless File.exist?("config/database.yml")
# system "cp config/database.yml.sample config/database.yml"
# end

puts "\n== Preparing database =="
system "bin/rake db:setup"

puts "\n== Removing old logs and tempfiles =="
system "rm -f log/*"
system "rm -rf tmp/cache"

puts "\n== Restarting application server =="
system "touch tmp/restart.txt"
end
17 changes: 17 additions & 0 deletions bin/spring
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env ruby

# This file loads spring without using Bundler, in order to be fast.
# It gets overwritten when you run the `spring binstub` command.

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

lockfile = Bundler::LockfileParser.new(Bundler.default_lockfile.read)
spring = lockfile.specs.detect { |spec| spec.name == "spring" }
if spring
Gem.use_paths Gem.dir, Bundler.bundle_path.to_s, *Gem.path
gem 'spring', spring.version
require 'spring/binstub'
end
end
4 changes: 4 additions & 0 deletions config.ru
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# This file is used by Rack-based servers to start the application.

require ::File.expand_path('../config/environment', __FILE__)
run Rails.application
9 changes: 6 additions & 3 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,12 @@ class Application < Rails::Application
# Do not swallow errors in after_commit/after_rollback callbacks.
config.active_record.raise_in_transactional_callbacks = true

# Use Rspec
config.generators do |g|
g.test_framework :rspec
config.autoload_paths << Rails.root.join('lib')

config.after_initialize do
Time.include CoreExtensions::Time::Season
Date.include CoreExtensions::Date::Season
String.include CoreExtensions::String::Name
end
end
end
22 changes: 0 additions & 22 deletions config/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,6 @@


Rails.application.configure do
# Only load the plugins named here, in the order given (default is alphabetical).
# :all can be used as a placeholder for all plugins not explicitly named
# config.plugins = [ :exception_notification, :ssl_requirement, :all ]

# Skip frameworks you're not going to use. To use Rails without a database,
# you must remove the Active Record framework.
# config.frameworks -= [ :active_record, :active_resource, :action_mailer ]

# Activate observers that should always be running
# config.active_record.observers = :cacher, :garbage_collector, :forum_observer

# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names.
# config.time_zone = 'UTC'

# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}')]
# config.i18n.default_locale = :de
# session key name
config.action_controller.session_store = :active_record_store
#ActionController::Base.session_options[:session_key] = 'audience1st_session_id'

config.after_initialize do
config.action_mailer.delivery_method = :test if Figaro.env.sandbox
end
Expand Down
3 changes: 1 addition & 2 deletions config/initializers/errors_as_html.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# rails3 this will become ActiveModel::Base
class ActiveRecord::Base
class ActiveModel::Base
def errors_as_html(sep = '<br/>')
errors.full_messages.join(sep)
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
require 'lib/date_time_extras'
class AddValidVouchersForBundles < ActiveRecord::Migration
def self.up
# for each Bundle voucher, add a valid-voucher that enables it
Expand Down
22 changes: 22 additions & 0 deletions lib/core_extensions/date/season.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module CoreExtensions
module Date
module Season
def self.included(base) ; base.extend(ClassMethods) ; end
def at_beginning_of_season(arg=nil)
self.to_time.at_beginning_of_season(arg).to_date
end
def at_end_of_season(arg=nil)
self.to_time.at_end_of_season(arg).to_date
end
def within_season?(arg)
self.to_time.within_season?(arg)
end
module ClassMethods
def from_year_month_day(hash)
now = Time.now
Date.new((hash[:year] || now.year).to_i, (hash[:month] || now.month).to_i, (hash[:day] || now.day).to_i)
end
end
end
end
end
Loading

0 comments on commit 524e046

Please sign in to comment.