Class: Locomotive::SiteService

Inherits:
Struct
  • Object
show all
Includes:
Concerns::ActivityService, Morphine
Defined in:
app/services/locomotive/site_service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Concerns::ActivityService

#track_activity, #without_tracking_activity

Instance Attribute Details

#accountObject

Returns the value of attribute account

Returns:

  • (Object)

    the current value of account


2
3
4
# File 'app/services/locomotive/site_service.rb', line 2

def 
  @account
end

Instance Method Details

#build_newObject


23
24
25
# File 'app/services/locomotive/site_service.rb', line 23

def build_new
  Site.new(handle: unique_handle)
end

#create(attributes, raise_if_not_valid = false) ⇒ Object


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/services/locomotive/site_service.rb', line 27

def create(attributes, raise_if_not_valid = false)
  if attributes[:handle].blank?
    attributes[:handle] = unique_handle
  end

  Site.new(attributes).tap do |site|
    site.created_by = 
    site.memberships.build account: , role: 'admin'

    success = raise_if_not_valid ? site.save! : site.save

    ActiveSupport::Notifications.instrument 'locomotive.site.created', account: , site: site

    track_activity 'site.created', site: site, parameters: { name: site.name } if success
  end
end

#create!(attributes) ⇒ Object


44
45
46
# File 'app/services/locomotive/site_service.rb', line 44

def create!(attributes)
  create(attributes, true)
end

#listObject


15
16
17
18
19
20
21
# File 'app/services/locomotive/site_service.rb', line 15

def list
  sorter = lambda do |site_a, site_b|
    site_a.name.downcase <=> site_b.name.downcase
  end

  .sites.order_by(:name.asc).to_a.sort(&sorter)
end

#update(site, attributes, raise_if_not_valid = false) ⇒ Object


48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/services/locomotive/site_service.rb', line 48

def update(site, attributes, raise_if_not_valid = false)
  site.attributes = attributes

  new_locales = site.locales_changed? ? site.locales - site.locales_was : nil
  previous_default_locale = site.default_locale_was

  (raise_if_not_valid ? site.save! : site.save).tap do |success|
    if success
      localize_pages_and_content_entries(site, new_locales, previous_default_locale)
    end
  end
end

#update!(site, attributes) ⇒ Object


61
62
63
# File 'app/services/locomotive/site_service.rb', line 61

def update!(site, attributes)
  update(site, attributes, true)
end