Skip to content

Commit 0ccb2be

Browse files
committed
add back models
1 parent c5d5e6b commit 0ccb2be

File tree

6 files changed

+129
-0
lines changed

6 files changed

+129
-0
lines changed

app/models/concerns/local_path.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module LocalPath
2+
def local_path
3+
Rails.root.join('files', self.class.name.underscore.pluralize, self.slug)
4+
end
5+
end

app/models/doc.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Doc < ApplicationRecord
2+
include FriendlyId, LocalPath
3+
4+
friendly_id :name, use: :slugged
5+
6+
belongs_to :project
7+
8+
has_many :doc_collection_memberships, dependent: :destroy
9+
has_many :doc_collections, through: :doc_collection_memberships
10+
11+
validates :tag, presence: true, uniqueness: { scope: :project_id }, inclusion: { in: ->(doc) { doc.project.tags }, if: -> { self.project.present? } }
12+
validates :project, presence: true
13+
14+
def local_git_path
15+
Rails.root.join('files', 'doc_gits', self.slug)
16+
end
17+
18+
def name
19+
if self.tag.blank?
20+
raise 'Cannot determine doc name without a tag.'
21+
end
22+
if self.project.nil?
23+
raise 'Cannot determine doc name without a project.'
24+
end
25+
version = Projects::ConvertTagsToVersions.call([self.tag])[self.tag]
26+
if version.nil?
27+
raise "Could not convert tag #{self.tag} to version."
28+
end
29+
[self.project.name, version].join(' ')
30+
end
31+
alias :to_s :name
32+
end

app/models/doc_collection.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class DocCollection < ApplicationRecord
2+
include FriendlyId, LocalPath
3+
4+
friendly_id :name, use: :slugged
5+
6+
has_many :doc_collection_memberships, dependent: :destroy
7+
has_many :docs, through: :doc_collection_memberships
8+
9+
validates :slug, presence: true
10+
validates :generated_with, presence: { if: -> { generated_at } }
11+
validates :generated_at, presence: { if: -> { uploaded_at } }
12+
13+
scope :generated, -> { where{(generated_at != nil) & (uploaded_at == nil)} }
14+
scope :uploaded, -> { where{uploaded_at != nil} }
15+
16+
accepts_nested_attributes_for :docs
17+
18+
def name
19+
docs = self.docs.presence || self.doc_collection_memberships.map(&:doc)
20+
docs.map(&:name).join(' & ')
21+
end
22+
alias :to_s :name
23+
24+
def zipfile
25+
self.local_path.to_s << '.zip'
26+
end
27+
28+
def generating?
29+
self.generated_at.nil?
30+
end
31+
32+
def uploading?
33+
!self.generating? && self.uploaded_at.nil?
34+
end
35+
36+
def uploaded?
37+
!!uploaded_at
38+
end
39+
end
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class DocCollectionMembership < ApplicationRecord
2+
belongs_to :doc
3+
belongs_to :doc_collection
4+
5+
validates :doc, presence: true
6+
validates :doc_id, uniqueness: { scope: :doc_collection_id }
7+
end

app/models/email_notification.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class EmailNotification
2+
include ActiveModel::Model
3+
4+
attr_accessor :email, :doc_collection_id
5+
6+
validates :email, presence: true, format: { with: /.+@.+\..+/, allow_blank: true }
7+
validates :doc_collection_id, presence: true
8+
9+
class << self
10+
def by_doc_collection(doc_collection)
11+
emails = Redis.current.smembers(cache_key(doc_collection.id))
12+
emails.map do |email|
13+
self.new email: email, doc_collection_id: doc_collection.id
14+
end
15+
end
16+
17+
def delete(doc_collection)
18+
Redis.current.del cache_key(doc_collection.id)
19+
end
20+
end
21+
22+
def save
23+
return false unless self.valid?
24+
Redis.current.sadd self.class.cache_key(self.doc_collection_id), self.email
25+
end
26+
27+
private
28+
29+
def self.cache_key(doc_collection_id)
30+
[
31+
'email_notifications',
32+
doc_collection_id
33+
].join(':')
34+
end
35+
end

app/models/project.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Project < ApplicationRecord
2+
include FriendlyId, LocalPath
3+
4+
friendly_id :name, use: :slugged
5+
6+
has_many :docs, dependent: :destroy
7+
has_many :doc_collections, through: :docs
8+
9+
validates :name, presence: true, uniqueness: true
10+
validates :git, presence: true, uniqueness: true
11+
end

0 commit comments

Comments
 (0)