Skip to content

Commit

Permalink
Merge 3468803 into 1bd6b4c
Browse files Browse the repository at this point in the history
  • Loading branch information
ShinjiTanimoto committed May 7, 2021
2 parents 1bd6b4c + 3468803 commit 8b9804e
Show file tree
Hide file tree
Showing 11 changed files with 186 additions and 10 deletions.
2 changes: 2 additions & 0 deletions app/jobs/sys/site_copy_job.rb
Expand Up @@ -29,6 +29,8 @@ def perform
name: @task.target_host_name,
host: @task.target_host_host,
domains: @task.target_host_domains,
subdir: @task.target_host_subdir,
parent_id: @task.target_host_parent_id,
max_name_length: @src_site.max_name_length
}
@dest_site = Cms::Site.create(dest_site_params.merge(group_ids: @src_site.group_ids))
Expand Down
2 changes: 1 addition & 1 deletion app/jobs/sys/site_import_job.rb
Expand Up @@ -132,7 +132,7 @@ def init_mapping
def import_dst_site
@dst_site.group_ids = convert_ids(@cms_groups_map, @src_site.group_ids)
@src_site.attributes.each do |key, val|
next if key =~ /^(created|updated|name|host|domains|.*_id|.*_ids)$/
next if key =~ /^(created|updated|name|host|domains|subdir|.*_id|.*_ids)$/
@dst_site[key] = val
end
@dst_site.save
Expand Down
31 changes: 30 additions & 1 deletion app/models/concerns/ss/model/site.rb
Expand Up @@ -29,10 +29,14 @@ class MultipleRootGroupsError < RuntimeError
permit_params :mypage_scheme, :mypage_domain
validates :name, presence: true, length: { maximum: 40 }
validates :host, uniqueness: true, presence: true, length: { minimum: 3, maximum: 16 }
validates :domains, domain: true
validates :domains, presence: true, domain: true
validates :subdir, presence: true, if: -> { parent.present? }
validates :parent_id, presence: true, if: -> { subdir.present? }

validate :validate_domains, if: ->{ domains.present? }

after_save :move_public_file

def domain
cur_domain || domains[0]
end
Expand Down Expand Up @@ -127,6 +131,31 @@ def validate_domains
end
end

def move_public_file
src_site = self.dup
src_site.host = host_was
src_site.subdir = subdir_was
src_site.parent_id = parent_id_was

return if src_site.host.blank?
return if path == src_site.path
return if Fs.exists?(path)
return if !Fs.exists?(src_site.path)

if path.include?(src_site.path)
temp_path = src_site.path.sub(self.class.root, "#{self.class.root}/temp")
Fs.mkdir_p(File.dirname(temp_path))
Fs.mv(src_site.path, src_site.path.sub(self.class.root, "#{self.class.root}/temp"))
FileUtils.rmdir(File.dirname(src_site.path), parents: true) if Dir.empty?(File.dirname(src_site.path))
Fs.mkdir_p(File.dirname(path))
FileUtils.rmdir(path, parents: true) if Fs.exists?(path) && Dir.empty?(path)
Fs.mv(temp_path, path) if Fs.exists?(temp_path)
else
Fs.mkdir_p(File.dirname(path))
Fs.mv(src_site.path, path)
end
end

class << self
def root
"#{Rails.public_path}/sites"
Expand Down
27 changes: 21 additions & 6 deletions app/models/sys/site_copy_task.rb
Expand Up @@ -6,28 +6,35 @@ class Sys::SiteCopyTask

default_scope ->{ where(name: "sys::site_copy") }

attr_accessor :target_host_domains_with_subdir

# field :results, type: Hash
field :target_host_name, type: String
field :target_host_host, type: String
field :target_host_domains, type: SS::Extensions::Words
field :target_host_subdir, type: String
belongs_to :target_host_parent, class_name: "SS::Site"
belongs_to :source_site, class_name: "SS::Site"
field :copy_contents, type: SS::Extensions::Words

permit_params :target_host_name, :target_host_host, :target_host_domains
permit_params :source_site_id
permit_params copy_contents: []
permit_params :target_host_name, :target_host_host, :target_host_domains, :target_host_subdir,
:target_host_parent_id, :source_site_id, copy_contents: []

validates :target_host_name, presence: true, length: { maximum: 40 }
validates :target_host_host, presence: true, length: { minimum: 3, maximum: 16 }
validate :validate_target_host_host, if: ->{ target_host_host.present? }
validates :target_host_domains, presence: true
validates :target_host_domains, presence: true, domain: true
validates :target_host_subdir, presence: true, if: ->{ target_host_parent.present? }
validates :target_host_parent_id, presence: true, if: ->{ target_host_subdir.present? }
validate :validate_target_host_domains, if: ->{ target_host_domains.present? }
validates :source_site_id, presence: true

def clear_params
self.target_host_name = nil
self.target_host_host = nil
self.target_host_domains = nil
self.target_host_subdir = nil
self.target_host_parent_id = nil
self.source_site_id = nil
self.copy_contents = nil
end
Expand All @@ -41,8 +48,16 @@ def validate_target_host_host
end

def validate_target_host_domains
return if target_host_domains.blank?
return if closed.present?
errors.add :target_host_domains, :duplicate if SS::Site.ne(id: id).any_in(domains: target_host_domains).exists?

self.target_host_domains = target_host_domains.uniq
self.target_host_domains_with_subdir = []
target_host_domains.each do |domain|
self.target_host_domains_with_subdir << (target_host_subdir.present? ? "#{domain}/#{target_host_subdir}" : domain)
end

if SS::Site.ne(id: id).any_in(domains_with_subdir: target_host_domains_with_subdir).exists?
errors.add :target_host_domains_with_subdir, :duplicate
end
end
end
10 changes: 10 additions & 0 deletions app/views/sys/site_copy/_confirm.html.erb
Expand Up @@ -14,6 +14,16 @@
<%= @item.target_host_domains %>
<%= f.hidden_field :target_host_domains %>
</dd>
<dt><%= @model.t :target_host_subdir %></dt>
<dd>
<%= @item.target_host_subdir %>
<%= f.hidden_field :target_host_subdir %>
</dd>
<dt><%= @model.t :target_host_parent_id %></dt>
<dd>
<%= @item.target_host_parent.try(:name) %>
<%= f.hidden_field :target_host_parent_id %>
</dd>
<dt><%= @model.t :source_site_id %></dt>
<dd>
<%= tryb { @item.source_site.name } %>
Expand Down
6 changes: 6 additions & 0 deletions app/views/sys/site_copy/_form.html.erb
Expand Up @@ -8,6 +8,12 @@
<dt><%= @model.t :target_host_domains %><%= @model.tt :target_host_domains %></dt>
<dd><%= f.text_field :target_host_domains %></dd>

<dt><%= @model.t :target_host_subdir %><%= @model.tt :target_host_subdir %></dt>
<dd><%= f.text_field :target_host_subdir %></dd>

<dt><%= @model.t :target_host_parent_id %><%= @model.tt :target_host_parent_id %></dt>
<dd><%= f.select :target_host_parent_id, options_from_collection_for_select(SS::Site.all, :id, :name), include_blank: true %></dd>

<dt><%= @model.t :source_site_id %><%= @model.tt :source_site_id %></dt>
<dd>
<%= f.hidden_field "source_site_id", value: "", class: "hidden-ids" %>
Expand Down
7 changes: 7 additions & 0 deletions config/locales/sys/ja.yml
Expand Up @@ -159,6 +159,9 @@ ja:
target_host_name: サイト名
target_host_host: ホスト名
target_host_domains: ドメイン
target_host_subdir: サブディレクトリ
target_host_parent_id: 親サイト
target_host_domains_with_subdir: サブディレクトリを含めたドメイン
source_site_id: 複製するサイト
copy_contents: 複製する項目
sys/mail_log:
Expand Down Expand Up @@ -220,6 +223,10 @@ ja:
- 複製するサイトのホスト名を入力します。
target_host_domains:
- 複製するサイトのドメインを入力します。
target_host_subdir:
- 複製するサイトのサブディレクトリを入力します。
target_host_parent_id:
- 複製するサイトの親サイトを選択します。
source_site_id:
- 複製の元となるサイトを選択します。
copy_contents:
Expand Down
1 change: 1 addition & 0 deletions spec/features/article/pages/related_page_in_body_spec.rb
Expand Up @@ -46,6 +46,7 @@
before do
site.domains = site2.domains
site.subdir = unique_id
site.parent_id = site2.id
site.save!
item.reload
end
Expand Down
4 changes: 2 additions & 2 deletions spec/features/members/agents/nodes/login_spec.rb
Expand Up @@ -3,8 +3,8 @@
describe 'members/agents/nodes/login', type: :feature, dbscope: :example, js: true do
describe "ss-2724" do
let(:root_site) { cms_site }
let(:site1) { create(:cms_site_subdir, domains: root_site.domains) }
let(:site2) { create(:cms_site_subdir, domains: root_site.domains) }
let(:site1) { create(:cms_site_subdir, domains: root_site.domains, parent_id: root_site.id) }
let(:site2) { create(:cms_site_subdir, domains: root_site.domains, parent_id: root_site.id) }

let(:site1_layout) { create_cms_layout(cur_site: site1) }
let(:site2_layout) { create_cms_layout(cur_site: site2) }
Expand Down
5 changes: 5 additions & 0 deletions spec/features/sys/site_copy_spec.rb
Expand Up @@ -14,6 +14,7 @@
let(:target_host_name) { unique_id }
let(:target_host_host) { unique_id }
let(:target_host_domain) { "#{unique_id}.example.jp" }
let(:target_host_subdir) { unique_id }

before do
login_sys_user
Expand All @@ -26,6 +27,8 @@
fill_in 'item[target_host_name]', with: target_host_name
fill_in 'item[target_host_host]', with: target_host_host
fill_in 'item[target_host_domains]', with: target_host_domain
fill_in 'item[target_host_subdir]', with: target_host_subdir
select site.name

check 'item_copy_contents_pages'
check 'item_copy_contents_files'
Expand Down Expand Up @@ -54,6 +57,8 @@
expect(task.target_host_name).to eq target_host_name
expect(task.target_host_host).to eq target_host_host
expect(task.target_host_domains).to include target_host_domain
expect(task.target_host_subdir).to eq target_host_subdir
expect(task.target_host_parent_id).to eq site.id
expect(task.source_site_id).to eq site.id
expect(task.copy_contents).to include('pages')
expect(task.copy_contents).to include('files')
Expand Down
101 changes: 101 additions & 0 deletions spec/features/sys/sites_spec.rb
Expand Up @@ -147,4 +147,105 @@
expect(SS::Site.all.count).to eq 0
end
end

describe "move" do
let!(:site1) { create(:sys_site, name: unique_id, host: unique_id, domains: unique_domain) }
let!(:site2) { create(:sys_site, name: unique_id, host: unique_id, domains: unique_domain) }
let!(:node1) { create :article_node_page, site_id: site1.id }
let!(:node2) { create :article_node_page, site_id: site2.id }
let!(:page1) { create :article_page, site_id: site1.id, cur_node: node1 }
let!(:page2) { create :article_page, site_id: site2.id, cur_node: node2 }

before { login_sys_user }

context "when host is changed" do
it do
visit sys_sites_path
click_on site1.name
expect(status_code).to eq 200

click_on I18n.t("ss.links.edit")
within "form#item-form" do
fill_in "item[host]", with: unique_id
click_on I18n.t('ss.buttons.save')
end

expect(page).to have_css('#notice', text: I18n.t("ss.notice.saved"))
page1.reload
page2.reload
expect(Fs.exists?(page1.path)).to be_truthy
expect(Fs.exists?(page2.path)).to be_truthy
end
end

context "when subdir and parent_id is changed" do
it do
visit sys_sites_path
click_on site2.name
expect(status_code).to eq 200

click_on I18n.t("ss.links.edit")
within "form#item-form" do
fill_in "item[subdir]", with: unique_id
select site1.name
click_on I18n.t('ss.buttons.save')
end

expect(page).to have_css('#notice', text: I18n.t("ss.notice.saved"))
page1.reload
page2.reload
expect(Fs.exists?(page1.path)).to be_truthy
expect(Fs.exists?(page2.path)).to be_truthy

click_on I18n.t("ss.links.edit")
within "form#item-form" do
fill_in "item[subdir]", with: ''
select ''
click_on I18n.t('ss.buttons.save')
end

expect(page).to have_css('#notice', text: I18n.t("ss.notice.saved"))
page1.reload
page2.reload
expect(Fs.exists?(page1.path)).to be_truthy
expect(Fs.exists?(page2.path)).to be_truthy
end
end

context "when host, subdir and parent_id is changed" do
it do
visit sys_sites_path
click_on site2.name
expect(status_code).to eq 200

click_on I18n.t("ss.links.edit")
within "form#item-form" do
fill_in "item[host]", with: unique_id
fill_in "item[subdir]", with: unique_id
select site1.name
click_on I18n.t('ss.buttons.save')
end

expect(page).to have_css('#notice', text: I18n.t("ss.notice.saved"))
page1.reload
page2.reload
expect(Fs.exists?(page1.path)).to be_truthy
expect(Fs.exists?(page2.path)).to be_truthy

click_on I18n.t("ss.links.edit")
within "form#item-form" do
fill_in "item[host]", with: unique_id
fill_in "item[subdir]", with: ''
select ''
click_on I18n.t('ss.buttons.save')
end

expect(page).to have_css('#notice', text: I18n.t("ss.notice.saved"))
page1.reload
page2.reload
expect(Fs.exists?(page1.path)).to be_truthy
expect(Fs.exists?(page2.path)).to be_truthy
end
end
end
end

0 comments on commit 8b9804e

Please sign in to comment.