Newer
Older
# == Schema Information
#
# Table name: communication_website_git_files
#
# id :uuid not null, primary key
# about_type :string not null
# identifier :string default("static")
# previous_path :string
# previous_sha :string
# created_at :datetime not null
# updated_at :datetime not null
# about_id :uuid not null
# website_id :uuid not null
#
# Indexes
#
# index_communication_website_git_files_on_website_id (website_id)
# index_communication_website_github_files_on_about (about_type,about_id)
#
# Foreign Keys
#
# fk_rails_... (website_id => communication_websites.id)
#
class Communication::Website::GitFile < ApplicationRecord
belongs_to :website, class_name: 'Communication::Website'
belongs_to :about, polymorphic: true
def self.sync(website, object, identifier)
git_file = where(website: website, about: object, identifier: identifier).first_or_create
website.git_repository.add_git_file git_file
end
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
def synced?
previous_path == path && previous_sha == sha
end
def path
about.send "git_path_#{identifier}"
end
def sha
# Git SHA-1 is calculated from the String "blob <length>\x00<contents>"
# Source: https://alblue.bandlem.com/2011/08/git-tip-of-week-objects.html
data = to_s
OpenSSL::Digest::SHA1.hexdigest "blob #{data.bytesize}\x00#{data}"
end
def to_s
ApplicationController.render(
template: "admin/#{about.class.name.underscore.pluralize}/#{identifier}",
layout: false,
assigns: { about.class.name.demodulize.downcase => about }
)
end
protected
# def add_media_to_batch(github)
# return unless manifest_data[:has_media] && about.respond_to?(:active_storage_blobs)
# about.active_storage_blobs.each { |blob| add_blob_to_batch(github, blob) }
# end
#
# def add_blob_to_batch(github, blob)
# github.add_to_batch github_blob_params(blob)
# end
#
# def remove_from_github
# return unless github.valid?
# github.remove github_path, github_remove_commit_message
# remove_media_from_github
# end
#
# def remove_media_from_github
# return unless manifest_data[:with_media] && about.respond_to?(:active_storage_blobs)
# about.active_storage_blobs.each { |blob| remove_blob_from_github(blob) }
# end
#
# def remove_blob_from_github(blob)
# github.remove github_blob_path(blob), github_blob_remove_commit_message
# end
#
# def github_params
# {
# path: manifest_data[:generated_path].call(self),
# previous_path: github_path,
# data: manifest_data[:data].call(self)
# }
# end
#
# def github_blob_params(blob)
# blob.analyze unless blob.analyzed?
# {
# path: github_blob_path(blob),
# data: ApplicationController.render(
# template: 'active_storage/blobs/static',
# layout: false,
# assigns: { blob: blob }
# )
# }
# end
#
# def github_blob_path(blob)
# "data/media/#{blob.id[0..1]}/#{blob.id}.yml"
# end
#
# def github_commit_message
# "[#{about.class.name.demodulize} - #{manifest_identifier}] Save #{about.to_s}"
# end
#
# def github_remove_commit_message
# "[#{about.class.name.demodulize} - #{manifest_identifier}] Remove #{about.to_s}"
# end
#
# def github_blob_remove_commit_message(blob)
# "[Medium] Remove ##{blob.id}"
# end
#
# def valid_for_publication?
# if about.respond_to?(:published)
# about.published?
# else
# true
# end
# end
end