Class: Adva::Static::Import::Model::Post
- Inherits:
-
Base
- Object
- Base
- Adva::Static::Import::Model::Post
show all
- Defined in:
- lib/adva/static/import/model/post.rb
Constant Summary
collapse
- PERMALINK =
%r((?:^|/)(\d{4})(?:\-|\/)(\d{1,2})(?:\-|\/)(\d{1,2})(?:\-|\/)(.*)$)
Instance Attribute Summary
Attributes inherited from Base
#source
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Base
#<=>, #==, #attribute_name?, #attributes, #body, #column_name?, #initialize, #load, #loadable, #model, #path, #updated_at, #updated_record
Class Method Details
.permalink?(path) ⇒ Boolean
15
16
17
|
# File 'lib/adva/static/import/model/post.rb', line 15
def permalink?(path)
path.to_s =~ PERMALINK
end
|
.recognize(sources) ⇒ Object
9
10
11
12
13
|
# File 'lib/adva/static/import/model/post.rb', line 9
def recognize(sources)
posts = sources.select { |source| source.path =~ PERMALINK }
sources.replace(sources - posts.map(&:self_and_parents).flatten)
posts.map { |post| new(post) }
end
|
.strip_permalink(source) ⇒ Object
19
20
21
|
# File 'lib/adva/static/import/model/post.rb', line 19
def strip_permalink(source)
Source.new(source.to_s.gsub(Post::PERMALINK, ''), source.root)
end
|
Instance Method Details
#attribute_names ⇒ Object
24
25
26
|
# File 'lib/adva/static/import/model/post.rb', line 24
def attribute_names
@attribute_names ||= [:site_id, :section_id, :title, :body, :published_at]
end
|
#path_tokens ⇒ Object
67
68
69
|
# File 'lib/adva/static/import/model/post.rb', line 67
def path_tokens
@path_tokens ||= source.to_s.gsub(/\.\w+$/, '').match(PERMALINK).to_a[1..-1]
end
|
#permalink ⇒ Object
63
64
65
|
# File 'lib/adva/static/import/model/post.rb', line 63
def permalink
@permalink ||= path_tokens.to_a[0..-2] << slug
end
|
#published_at ⇒ Object
71
72
73
|
# File 'lib/adva/static/import/model/post.rb', line 71
def published_at
@published_at ||= DateTime.civil(*permalink[0..-2].map(&:to_i))
end
|
#record ⇒ Object
28
29
30
|
# File 'lib/adva/static/import/model/post.rb', line 28
def record
@record ||= section.posts.by_permalink(*permalink).all.first || section.posts.build
end
|
#section ⇒ Object
36
37
38
|
# File 'lib/adva/static/import/model/post.rb', line 36
def section
@section ||= Blog.new(section_source).record
end
|
#section_id ⇒ Object
40
41
42
|
# File 'lib/adva/static/import/model/post.rb', line 40
def section_id
section.id.to_s
end
|
#section_source ⇒ Object
44
45
46
47
48
49
50
51
52
53
|
# File 'lib/adva/static/import/model/post.rb', line 44
def section_source
@section_source ||= begin
source = self.class.strip_permalink(self.source)
if source.path.present?
source.find_or_self
else
Source.new(source.join('index'), source.root).find_or_self
end
end
end
|
#site_id ⇒ Object
32
33
34
|
# File 'lib/adva/static/import/model/post.rb', line 32
def site_id
section.site_id.to_s
end
|
#slug ⇒ Object
55
56
57
|
# File 'lib/adva/static/import/model/post.rb', line 55
def slug
@slug ||= SimpleSlugs::Slug.new(title).to_s
end
|
#title ⇒ Object
59
60
61
|
# File 'lib/adva/static/import/model/post.rb', line 59
def title
@title ||= path_tokens.last.titleize
end
|