Class: Landable::Template

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Librarian, TableName
Defined in:
app/models/landable/template.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Librarian

#nuke!, #reactivate

Instance Attribute Details

#temp_authorObject

attributes



7
8
9
# File 'app/models/landable/template.rb', line 7

def temp_author
  @temp_author
end

Class Method Details

.create_from_partials!Object



77
78
79
# File 'app/models/landable/template.rb', line 77

def create_from_partials!
  Partial.all.map(&:to_template)
end

Instance Method Details

#deactivateObject



28
29
30
31
32
# File 'app/models/landable/template.rb', line 28

def deactivate
  publish!(author_id: temp_author.id, notes: 'This template has been trashed')

  super
end

#name=(val) ⇒ Object



34
35
36
37
# File 'app/models/landable/template.rb', line 34

def name=(val)
  self[:name] = val
  self[:slug] ||= (val && val.underscore.gsub(/[^\w_]/, '_').gsub(/_{2,}/, '_'))
end

#partial?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'app/models/landable/template.rb', line 39

def partial?
  file.present?
end

#publish!(options) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'app/models/landable/template.rb', line 43

def publish!(options)
  transaction do
    published_revision.unpublish! if published_revision
    revision = revisions.create! options
    update_attributes!(published_revision: revision, is_publishable: false)

    # Republish Templates Pages Last Page Revision
    republish_associated_pages(options)
  end
end

#republish_associated_pages(options) ⇒ Object



54
55
56
57
58
59
# File 'app/models/landable/template.rb', line 54

def republish_associated_pages(options)
  options[:template] = name
  pages.each do |page|
    page.republish!(options) if page.published?
  end
end

#revert_to!(revision) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'app/models/landable/template.rb', line 61

def revert_to!(revision)
  self.name          = revision.name
  self.body          = revision.body
  self.description   = revision.description
  self.category_id   = revision.category_id
  self.slug          = revision.slug

  save!
end

#slug_has_no_spacesObject



71
72
73
74
# File 'app/models/landable/template.rb', line 71

def slug_has_no_spaces
  return unless slug =~ /\s/ # check if whitespace
  self.slug = slug.underscore.gsub(/[^\w_]/, '_').gsub(/_{2,}/, '_')
end