Class: Simplec::Page

Inherits:
ApplicationRecord show all
Includes:
Normalizers
Defined in:
app/models/simplec/page.rb

Defined Under Namespace

Modules: Normalizers Classes: AlreadyLinkedEmbeddedImage

Constant Summary collapse

FILE_FIELDS =

TODO Document FILE_FIELDS constant

[:file, :image].freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Normalizers

#slug, #slug=

Class Method Details

.field(name, options = {}) ⇒ Object

Define a field on the page.

  • name - name of field

  • options - :string (default), :text, :editor, :file, :image :string - yields a text input :text - yields a textarea :editor - yields a summernote editor :file - yields a file field :image - yields a file field with image preview

There is as template for each type for customization located in:

app/views/shared/fields/_TYPE.html.erb

Defines a field on a subclass. This creates a getter and setter for the name passed in. The options are used when building the administration forms.

Regular dragonfly validations are available on :file and :image fields. markevans.github.io/dragonfly/models#validations



81
82
83
84
85
86
87
88
89
90
# File 'app/models/simplec/page.rb', line 81

def self.field(name, options={})
	fields[name] = {name: name, type: :string}.merge(options)
	if FILE_FIELDS.member?(fields[name][:type])
		dragonfly_accessor name
		data_field :"#{name}_uid"
		data_field :"#{name}_name"
	else
		data_field(name)
	end
end

.field_names(type = nil) ⇒ Object

Return names of fields.

type: :file, is the only option



111
112
113
114
115
116
117
118
119
# File 'app/models/simplec/page.rb', line 111

def self.field_names(type=nil)
	_fields = case type
						when :file
							fields.select {|k, v| FILE_FIELDS.member?(v[:type])}
						else
							fields
						end
	_fields.keys
end

.fieldsObject

List fields



94
95
96
# File 'app/models/simplec/page.rb', line 94

def self.fields
	@fields ||= Hash.new
end

.type(type) ⇒ Object

Return a constantized type, whitelisted by known subclasses.



100
101
102
103
104
105
# File 'app/models/simplec/page.rb', line 100

def self.type(type)
    ::Page rescue raise '::Page not defined, define it in app/models'
	raise 'Unsupported Page Type; define in app/models/page/' unless ::Page.subclasses.map(&:name).
		member?(type)
	type.constantize
end

Instance Method Details

#build_pathObject

Before validation hook.

Build the path of the page to be used in routing.



142
143
144
145
# File 'app/models/simplec/page.rb', line 142

def build_path
	_pages = self.parents.reverse + [self]
	self.path = "/#{_pages.map(&:slug).reject(&:blank?).join('/')}"
end

#field_optionsObject

Return field options for building forms.



123
124
125
# File 'app/models/simplec/page.rb', line 123

def field_options
	self.class.fields.values
end

#find_embedded_imagesObject



156
157
158
159
160
161
162
# File 'app/models/simplec/page.rb', line 156

def find_embedded_images
	text = self.fields.values.join(' ')
	matches = text.scan(/ei=([^&]*)/)
	encoded_ids = matches.map(&:first)
	ids = encoded_ids.map { |eid| Base64.urlsafe_decode64(URI.unescape(eid)) }
	EmbeddedImage.includes(:embeddable).find(ids)
end

#layoutsObject



173
174
175
# File 'app/models/simplec/page.rb', line 173

def layouts
	@layouts ||= Subdomain.new.layouts
end


164
165
166
167
168
169
170
171
# File 'app/models/simplec/page.rb', line 164

def link_embedded_images!
	images = self.find_embedded_images
	images.each do |image|
		raise AlreadyLinkedEmbeddedImage if image.embeddable &&
			image.embeddable != self
		image.update!(embeddable: self)
	end
end

#match_parent_subdomainObject

Before validation hook

All pages need to have a matching subdomain to parent page



151
152
153
154
# File 'app/models/simplec/page.rb', line 151

def match_parent_subdomain
	return unless self.parent
	self.subdomain = self.parent.subdomain
end

#parentsObject

List parents, closest to furthest.



129
130
131
132
133
134
135
136
# File 'app/models/simplec/page.rb', line 129

def parents
	page, parents = self, Array.new
	while page.parent
		page = page.parent
		parents << page
	end
	parents
end