Make open source

This commit is contained in:
John Lees-Miller
2018-10-27 16:46:26 +01:00
commit 6e82ba528b
163 changed files with 3912 additions and 0 deletions

9
app/models/admin_user.rb Normal file
View File

@ -0,0 +1,9 @@
#
# ActiveAdmin console user.
#
class AdminUser < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable,
:recoverable, :rememberable, :trackable, :validatable
end

View File

@ -0,0 +1,3 @@
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
end

7
app/models/attendee.rb Normal file
View File

@ -0,0 +1,7 @@
#
# A guest or plus one.
#
class Attendee < ApplicationRecord
scope :diet?, -> { where.not(diet: nil) }
scope :child?, -> { where(child: true) }
end

View File

View File

@ -0,0 +1,27 @@
#
# Make a model findable only when a secure token is provided.
#
module FindableWithToken
extend ActiveSupport::Concern
ID_TOKEN_RX = /\A(\d+)-(\w+)\z/
included do
has_secure_token
def to_param
id ? "#{id}-#{token}" : nil
end
def self.find_by_id_token(id_token)
raise ActiveRecord::RecordNotFound unless id_token =~ ID_TOKEN_RX
id = Regexp.last_match(1)
token = Regexp.last_match(2)
record = find(id)
raise ActiveRecord::RecordNotFound unless
ActiveSupport::SecurityUtils.secure_compare(record.token, token)
record
end
end
end

41
app/models/guest.rb Normal file
View File

@ -0,0 +1,41 @@
#
# A primary guest.
#
class Guest < ApplicationRecord
include FindableWithToken
auto_strip_attributes :email, :first_name, :last_name, :diet, :songs, :notes
validates :email, presence: true, uniqueness: true
validates :email, format: Devise.email_regexp, allow_blank: true
validates :first_name, presence: true, if: :persisted?
validates :first_name, length: { maximum: 1024 }
validates :last_name, presence: true, if: :persisted?
validates :last_name, length: { maximum: 1024 }
def name
"#{first_name} #{last_name}"
end
def name_with_email
"#{name} <#{email}>"
end
# Don't allow long or odd names in emails; may be spam.
def email_safe_salutation
return 'Hello,' if
first_name.blank? || first_name !~ /\A[\p{Word}\s'-]{1,30}\z/i
"Dear #{first_name},"
end
validates :diet, length: { maximum: 8192 }
validates :songs, length: { maximum: 8192 }
validates :notes, length: { maximum: 8192 }
has_many :plus_ones, dependent: :destroy
scope :confirmed, -> { where.not(confirmed_at: nil) }
scope :attending, -> { confirmed.where(attending: true) }
scope :not_attending, -> { confirmed.where(attending: false) }
end

19
app/models/plus_one.rb Normal file
View File

@ -0,0 +1,19 @@
#
# An extra guest.
#
class PlusOne < ApplicationRecord
belongs_to :guest
auto_strip_attributes :first_name, :last_name, :diet
validates :first_name, presence: true
validates :first_name, length: { maximum: 1024 }
validates :last_name, presence: true
validates :last_name, length: { maximum: 1024 }
def name
"#{first_name} #{last_name}"
end
validates :diet, length: { maximum: 8192 }
end