Initial commit
This commit is contained in:
0
app/models/concerns/.keep
Normal file
0
app/models/concerns/.keep
Normal file
40
app/models/concerns/archivable.rb
Normal file
40
app/models/concerns/archivable.rb
Normal file
@@ -0,0 +1,40 @@
|
||||
module Archivable
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
included do
|
||||
scope :active, -> { where("updated_at > ?", Config.active_threshold_date) }
|
||||
scope :inactive, -> { where(updated_at: Config.inactive_date_range) }
|
||||
scope :archived, -> { where("updated_at < ?", Config.archive_threshold_date) }
|
||||
|
||||
def archive_status
|
||||
if updated_at > Config.active_threshold_date
|
||||
:active
|
||||
elsif updated_at.between? Config.archive_threshold_date, Config.active_threshold_date
|
||||
:inactive
|
||||
else
|
||||
:archived
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
class Config
|
||||
ACTIVE_THRESHOLD_IN_DAYS = 90
|
||||
ARCHIVE_THRESHOLD_IN_DAYS = 365
|
||||
|
||||
class << self
|
||||
def active_threshold_date
|
||||
ACTIVE_THRESHOLD_IN_DAYS.days.ago.beginning_of_day
|
||||
end
|
||||
|
||||
def archive_threshold_date
|
||||
ARCHIVE_THRESHOLD_IN_DAYS.days.ago.end_of_day
|
||||
end
|
||||
|
||||
def inactive_date_range
|
||||
archive_threshold_date..active_threshold_date
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
10
app/models/concerns/confirmable.rb
Normal file
10
app/models/concerns/confirmable.rb
Normal file
@@ -0,0 +1,10 @@
|
||||
module Confirmable
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
included do
|
||||
has_many :video_release_confirmations, as: :releasable, dependent: :destroy
|
||||
has_many :confirmed_videos, source: :video, through: :video_release_confirmations
|
||||
|
||||
scope :appearing_in, -> (video) { joins(:confirmed_videos).where(videos: { id: video }) }
|
||||
end
|
||||
end
|
||||
15
app/models/concerns/contractable.rb
Normal file
15
app/models/concerns/contractable.rb
Normal file
@@ -0,0 +1,15 @@
|
||||
module Contractable
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
included do
|
||||
has_one_attached :contract
|
||||
|
||||
validates :contract, content_type: ["application/pdf"]
|
||||
|
||||
scope :having_contract_attached, -> (release_ids) { left_joins(:contract_attachment).where(active_storage_attachments: { record_id: release_ids }).group(:id).having("COUNT(active_storage_attachments) > 0") }
|
||||
|
||||
def contract_file_name
|
||||
"#{project.name.parameterize}_#{contract_template.release_type}_#{(signed_at || created_at).strftime("%Y.%m.%d")}_#{release_number}_#{name.parameterize}"
|
||||
end
|
||||
end
|
||||
end
|
||||
24
app/models/concerns/exploitable.rb
Normal file
24
app/models/concerns/exploitable.rb
Normal file
@@ -0,0 +1,24 @@
|
||||
module Exploitable
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
FIELDS = [:applicable_medium, :territory, :term, :restriction]
|
||||
|
||||
included do
|
||||
FIELDS.each do |field|
|
||||
belongs_to field, optional: true
|
||||
|
||||
define_method "#{field}_value" do
|
||||
if respond_to?(:contract_template) && contract_template.present?
|
||||
# If contract template is present, use the value from there
|
||||
contract_template.public_send("#{field}_value")
|
||||
else
|
||||
# Otherwise use the value of the label or the text
|
||||
field_assoc = public_send(field)
|
||||
if field_assoc
|
||||
field_assoc.other? ? public_send("#{field}_text") : field_assoc.label
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
45
app/models/concerns/filterable.rb
Normal file
45
app/models/concerns/filterable.rb
Normal file
@@ -0,0 +1,45 @@
|
||||
module Filterable
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
class_methods do
|
||||
def filterable_by(*scopes)
|
||||
@@filterer = Filterer.new(scopes)
|
||||
end
|
||||
|
||||
def filter(name)
|
||||
@@filterer.filter(name, self)
|
||||
end
|
||||
|
||||
def filter!(name)
|
||||
@@filterer.filter!(name, self)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
class Filterer
|
||||
def initialize(scopes)
|
||||
@scopes = Array.wrap(scopes).map(&:to_s)
|
||||
end
|
||||
|
||||
# Returns the original scope if filter has not been whitelisted
|
||||
def filter(name, scope)
|
||||
return scope.all unless filter_is_whitelisted?(name)
|
||||
|
||||
scope.send(name)
|
||||
end
|
||||
|
||||
# Raises an exception unless filter has been whitelisted
|
||||
def filter!(name, scope)
|
||||
raise "Cannot filter #{scope} by `#{name}`" unless filter_is_whitelisted?(name)
|
||||
|
||||
scope.send(name)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def filter_is_whitelisted?(name)
|
||||
@scopes.include?(name)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
8
app/models/concerns/freeformable.rb
Normal file
8
app/models/concerns/freeformable.rb
Normal file
@@ -0,0 +1,8 @@
|
||||
module Freeformable
|
||||
extend ActiveSupport::Concern
|
||||
included do
|
||||
def other?
|
||||
label == "Other"
|
||||
end
|
||||
end
|
||||
end
|
||||
20
app/models/concerns/guardian_name.rb
Normal file
20
app/models/concerns/guardian_name.rb
Normal file
@@ -0,0 +1,20 @@
|
||||
module GuardianName
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
included do
|
||||
def guardian_name
|
||||
"#{guardian_first_name} #{guardian_last_name}".titleize
|
||||
end
|
||||
|
||||
def guardian_name=(value)
|
||||
if value.include?(' ')
|
||||
split = value.split(" ", 2)
|
||||
self.guardian_first_name = split.first
|
||||
self.guardian_last_name = split.last
|
||||
else
|
||||
self.guardian_first_name = value
|
||||
self.guardian_last_name = "(Not Given)"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
9
app/models/concerns/guardian_photoable.rb
Normal file
9
app/models/concerns/guardian_photoable.rb
Normal file
@@ -0,0 +1,9 @@
|
||||
module GuardianPhotoable
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
included do
|
||||
has_one_attached :guardian_photo
|
||||
|
||||
validates :guardian_photo, content_type: ["image/png", "image/jpeg"]
|
||||
end
|
||||
end
|
||||
7
app/models/concerns/notable.rb
Normal file
7
app/models/concerns/notable.rb
Normal file
@@ -0,0 +1,7 @@
|
||||
module Notable
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
included do
|
||||
has_many :notes, as: :notable, dependent: :destroy
|
||||
end
|
||||
end
|
||||
20
app/models/concerns/person_name.rb
Normal file
20
app/models/concerns/person_name.rb
Normal file
@@ -0,0 +1,20 @@
|
||||
module PersonName
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
included do
|
||||
def person_name
|
||||
"#{person_first_name} #{person_last_name}".titleize
|
||||
end
|
||||
|
||||
def person_name=(value)
|
||||
if value.include?(' ')
|
||||
split = value.split(" ", 2)
|
||||
self.person_first_name = split.first
|
||||
self.person_last_name = split.last
|
||||
else
|
||||
self.person_first_name = value
|
||||
self.person_last_name = "(Not Given)"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
13
app/models/concerns/photoable.rb
Normal file
13
app/models/concerns/photoable.rb
Normal file
@@ -0,0 +1,13 @@
|
||||
module Photoable
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
included do
|
||||
has_many_attached :photos
|
||||
|
||||
validates :photos, content_type: ["image/png", "image/jpeg"]
|
||||
end
|
||||
|
||||
def photo
|
||||
MainPhoto.new(photos.first)
|
||||
end
|
||||
end
|
||||
70
app/models/concerns/releasable.rb
Normal file
70
app/models/concerns/releasable.rb
Normal file
@@ -0,0 +1,70 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Releasable
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
included do
|
||||
belongs_to :project, touch: true
|
||||
belongs_to :contract_template, optional: true
|
||||
end
|
||||
|
||||
def release_number
|
||||
@release_number ||= ReleaseNumber.new(self).value
|
||||
end
|
||||
|
||||
def signed_on
|
||||
if respond_to?(:signed_at) && signed_at.present?
|
||||
signed_at.strftime('%D')
|
||||
else
|
||||
created_at.strftime('%D')
|
||||
end
|
||||
end
|
||||
|
||||
def fill_with_dummy_data(previewed_contract_template)
|
||||
self.contract_template = previewed_contract_template
|
||||
self.project_id = contract_template.project_id
|
||||
self.created_at = DateTime.current
|
||||
self.name = 'Dummy Name' if has_attribute? :name
|
||||
self.person_first_name = 'Dummy' if has_attribute? :person_first_name
|
||||
self.person_last_name = 'Person' if has_attribute? :person_last_name
|
||||
if has_attribute? :person_date_of_birth
|
||||
self.person_date_of_birth = DateTime.current
|
||||
end
|
||||
if has_attribute? :person_address_street1
|
||||
self.person_address_street1 = 'Street 1'
|
||||
end
|
||||
if has_attribute? :person_address_street2
|
||||
self.person_address_street2 = 'Street 2'
|
||||
end
|
||||
self.person_address_city = 'City' if has_attribute? :person_address_city
|
||||
self.person_address_state = 'State' if has_attribute? :person_address_state
|
||||
self.person_address_zip = 12_345 if has_attribute? :person_address_zip
|
||||
if has_attribute? :person_address_country
|
||||
self.person_address_country = 'Country'
|
||||
end
|
||||
if has_attribute? :person_address
|
||||
self.person_address = 'Street 1, Street 2, City, State 12345, Country'
|
||||
end
|
||||
self.person_phone = '00 111 222 333 4444' if has_attribute? :person_phone
|
||||
self.person_email = 'email@email.com' if has_attribute? :person_email
|
||||
self.description = 'Dummy description' if has_attribute? :description
|
||||
self.filming_started_on = DateTime.current if has_attribute? :filming_started_on
|
||||
self.filming_ended_on = DateTime.current if has_attribute? :filming_ended_on
|
||||
return if contract_template.guardian_clause.blank?
|
||||
|
||||
self.guardian_name = 'Guardian name' if has_attribute? :guardian_name
|
||||
if has_attribute? :guardian_address
|
||||
self.guardian_address = 'Street 3, Street 4, City-2, State-2 112233, Country-2'
|
||||
end
|
||||
self.guardian_address_street1 = 'Street 3' if has_attribute? :guardian_address_street1
|
||||
self.guardian_address_street2 = 'Street 4' if has_attribute? :guardian_address_street2
|
||||
self.guardian_address_city = 'City-2' if has_attribute? :guardian_address_city
|
||||
self.guardian_address_state = 'State-2' if has_attribute? :guardian_address_state
|
||||
self.guardian_address_zip = '112233' if has_attribute? :guardian_address_zip
|
||||
self.guardian_address_country = 'Country-2' if has_attribute? :guardian_address_country
|
||||
self.guardian_phone = '00 123 456 7890' if has_attribute? :guardian_phone
|
||||
self.guardian_email = 'guardian.email@mail.com' if has_attribute? :guardian_email
|
||||
self.minor = true if has_attribute? :minor
|
||||
|
||||
end
|
||||
end
|
||||
27
app/models/concerns/searchable.rb
Normal file
27
app/models/concerns/searchable.rb
Normal file
@@ -0,0 +1,27 @@
|
||||
module Searchable
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
included do
|
||||
include PgSearch
|
||||
end
|
||||
|
||||
class_methods do
|
||||
def searchable_on(fields)
|
||||
search_opts = {
|
||||
against: fields,
|
||||
associated_against: {
|
||||
notes: [:content],
|
||||
tags: [:name],
|
||||
internal_tags: [:name]
|
||||
},
|
||||
using: {
|
||||
tsearch: { any_word: true, prefix: true },
|
||||
trigram: {},
|
||||
dmetaphone: { any_word: true },
|
||||
}
|
||||
}
|
||||
|
||||
send(:pg_search_scope, :search, search_opts)
|
||||
end
|
||||
end
|
||||
end
|
||||
30
app/models/concerns/signable.rb
Normal file
30
app/models/concerns/signable.rb
Normal file
@@ -0,0 +1,30 @@
|
||||
module Signable
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
included do
|
||||
include ActiveStorageSupport::SupportForBase64
|
||||
|
||||
has_one_base64_attached :signature
|
||||
|
||||
scope :having_a_signature, -> { left_joins(:signature_attachment).group(:id).having("COUNT(active_storage_attachments) > 0") }
|
||||
scope :having_no_signature, -> { left_joins(:signature_attachment).group(:id).having("COUNT(active_storage_attachments) = 0") }
|
||||
|
||||
# Create some descriptive aliases for scopes above relating to native vs. non-native
|
||||
scope :native, -> { having_a_signature }
|
||||
scope :non_native, -> { having_no_signature }
|
||||
end
|
||||
|
||||
def native?
|
||||
signature.attached?
|
||||
end
|
||||
|
||||
def signature_base64
|
||||
return nil
|
||||
end
|
||||
|
||||
def signature_base64=(data_uri)
|
||||
return if data_uri.blank?
|
||||
|
||||
signature.attach(data: data_uri, filename: "signature.png", content_type: "image/png", identify: "false")
|
||||
end
|
||||
end
|
||||
19
app/models/concerns/syncable.rb
Normal file
19
app/models/concerns/syncable.rb
Normal file
@@ -0,0 +1,19 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Syncable
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
def as_json(params = {})
|
||||
json = super(params)
|
||||
json.each do |key, value|
|
||||
if key == "id"
|
||||
json[key] = value.to_s
|
||||
end
|
||||
end
|
||||
{
|
||||
id: id.to_s,
|
||||
type: model_name.param_key,
|
||||
attributes: json
|
||||
}
|
||||
end
|
||||
end
|
||||
9
app/models/concerns/taggable.rb
Normal file
9
app/models/concerns/taggable.rb
Normal file
@@ -0,0 +1,9 @@
|
||||
module Taggable
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
included do
|
||||
acts_as_taggable_on :internal_tags, :tags
|
||||
|
||||
enum tagging_status: %i[ pending started finished failed ], _prefix: "tagging"
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user