Class: ContentBlock

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/content_block.rb

Constant Summary collapse

NAME_REGISTRY =

The keys in this registry are “public” names for collaborator objects, and the values are reserved names of ContentBlock instances, which Hyrax uses as identifiers. Values also correspond to names of messages that can be sent to the ContentBlock class to return defined ContentBlock instances.

{
  marketing: :marketing_text,
  researcher: :featured_researcher,
  announcement: :announcement_text,
  about: :about_page,
  help: :help_page,
  terms: :terms_page,
  agreement: :agreement_page
}.freeze

Class Method Summary collapse

Class Method Details

.about_pageObject



54
55
56
# File 'app/models/content_block.rb', line 54

def about_page
  find_or_create_by(name: 'about_page')
end

.about_page=(value) ⇒ Object



58
59
60
# File 'app/models/content_block.rb', line 58

def about_page=(value)
  about_page.update(value: value)
end

.agreement_pageObject



62
63
64
65
# File 'app/models/content_block.rb', line 62

def agreement_page
  find_by(name: 'agreement_page') ||
    create(name: 'agreement_page', value: default_agreement_text)
end

.agreement_page=(value) ⇒ Object



67
68
69
# File 'app/models/content_block.rb', line 67

def agreement_page=(value)
  agreement_page.update(value: value)
end

.announcement_textObject



38
39
40
# File 'app/models/content_block.rb', line 38

def announcement_text
  find_or_create_by(name: 'announcement_text')
end

.announcement_text=(value) ⇒ Object



42
43
44
# File 'app/models/content_block.rb', line 42

def announcement_text=(value)
  announcement_text.update(value: value)
end

.default_agreement_textObject



88
89
90
91
92
93
94
# File 'app/models/content_block.rb', line 88

def default_agreement_text
  ERB.new(
    IO.read(
      Hyrax::Engine.root.join('app', 'views', 'hyrax', 'content_blocks', 'templates', 'agreement.html.erb')
    )
  ).result
end

.default_terms_textObject



96
97
98
99
100
101
102
# File 'app/models/content_block.rb', line 96

def default_terms_text
  ERB.new(
    IO.read(
      Hyrax::Engine.root.join('app', 'views', 'hyrax', 'content_blocks', 'templates', 'terms.html.erb')
    )
  ).result
end


46
47
48
# File 'app/models/content_block.rb', line 46

def featured_researcher
  find_or_create_by(name: 'featured_researcher')
end


50
51
52
# File 'app/models/content_block.rb', line 50

def featured_researcher=(value)
  featured_researcher.update(value: value)
end

.for(key) ⇒ Object

NOTE: method defined outside the metaclass wrapper below because ‘for` is a reserved word in Ruby.

Raises:

  • (ArgumentError)


19
20
21
22
23
# File 'app/models/content_block.rb', line 19

def self.for(key)
  key = key.respond_to?(:to_sym) ? key.to_sym : key
  raise ArgumentError, "#{key} is not a ContentBlock name" unless whitelisted?(key)
  ContentBlock.public_send(NAME_REGISTRY[key])
end

.help_pageObject



71
72
73
# File 'app/models/content_block.rb', line 71

def help_page
  find_or_create_by(name: 'help_page')
end

.help_page=(value) ⇒ Object



75
76
77
# File 'app/models/content_block.rb', line 75

def help_page=(value)
  help_page.update(value: value)
end

.marketing_textObject



30
31
32
# File 'app/models/content_block.rb', line 30

def marketing_text
  find_or_create_by(name: 'marketing_text')
end

.marketing_text=(value) ⇒ Object



34
35
36
# File 'app/models/content_block.rb', line 34

def marketing_text=(value)
  marketing_text.update(value: value)
end

.terms_pageObject



79
80
81
82
# File 'app/models/content_block.rb', line 79

def terms_page
  find_by(name: 'terms_page') ||
    create(name: 'terms_page', value: default_terms_text)
end

.terms_page=(value) ⇒ Object



84
85
86
# File 'app/models/content_block.rb', line 84

def terms_page=(value)
  terms_page.update(value: value)
end

.whitelisted?(key) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
# File 'app/models/content_block.rb', line 26

def whitelisted?(key)
  NAME_REGISTRY.include?(key)
end