Class: MarkdownRecord::ContentFragment

Inherits:
Base
  • Object
show all
Includes:
PathUtilities
Defined in:
app/models/markdown_record/content_fragment.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from PathUtilities

#base_content_root_name, #base_rendered_path, #base_rendered_root, #clean_path, #erb_locals_from_path, #fragment_attributes_from_path, #full_path_to_parts, #path_to_fragment_id, #remove_prefix, #rendered_path, #scoped_id_to_parts, #to_scoped_id

Methods inherited from Base

json_klass, #model_name, #to_key

Methods included from Associations

#children, #class_siblings, #fragment, #siblings

Constructor Details

#initialize(attributes = nil, options = {}) ⇒ ContentFragment

Returns a new instance of ContentFragment.



10
11
12
13
14
15
16
# File 'app/models/markdown_record/content_fragment.rb', line 10

def initialize(attributes = nil, options = {})
  super

  if self.meta.class == Hash
    self.meta = self.meta.with_indifferent_access
  end
end

Class Method Details

.new_association(base_filters = {}, search_filters = {}) ⇒ Object

Override the new_association method on MarkdownRecord::Base to force all association queries to only look for fragments



20
21
22
# File 'app/models/markdown_record/content_fragment.rb', line 20

def self.new_association(base_filters = {}, search_filters = {})
  MarkdownRecord::Association.new(base_filters, search_filters).fragmentize
end

Instance Method Details

#ancestorsObject

Associations



74
75
76
# File 'app/models/markdown_record/content_fragment.rb', line 74

def ancestors
  ancestors_from(::MarkdownRecord.config.content_root.basename.to_s)
end

#ancestors_from(ancestor) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'app/models/markdown_record/content_fragment.rb', line 89

def ancestors_from(ancestor)
  ancestor = self.class.find(ancestor) if ancestor.is_a?(String)
  
  parents = []
  current_parent = self.class.find(subdirectory)

  while current_parent
    if current_parent.id == ancestor.id
      parents.unshift(current_parent)
      current_parent = nil
    else
      parents.unshift(current_parent)
      current_parent = self.class.find(current_parent.subdirectory)
    end
  end

  parents
end

#exists?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'app/models/markdown_record/content_fragment.rb', line 37

def exists?
  json_exists? || html_exists?
end

#find_relative(relative_id) ⇒ Object



24
25
26
27
# File 'app/models/markdown_record/content_fragment.rb', line 24

def find_relative(relative_id)
  real_id = Pathname.new(id).parent.join(relative_id).to_s
  self.class.find(real_id)
end

#fragment_idObject



29
30
31
# File 'app/models/markdown_record/content_fragment.rb', line 29

def fragment_id
  id
end

#html_exists?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'app/models/markdown_record/content_fragment.rb', line 45

def html_exists?
  Pathname.new(html_path).exist?
end

#html_pathObject



65
66
67
# File 'app/models/markdown_record/content_fragment.rb', line 65

def html_path
  path("html")
end

#html_routeObject



69
70
71
# File 'app/models/markdown_record/content_fragment.rb', line 69

def html_route
  route("html", id)
end

#json_exists?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'app/models/markdown_record/content_fragment.rb', line 41

def json_exists?
  Pathname.new(json_path).exist?
end

#json_pathObject



57
58
59
# File 'app/models/markdown_record/content_fragment.rb', line 57

def json_path
  path("json")
end

#json_routeObject



61
62
63
# File 'app/models/markdown_record/content_fragment.rb', line 61

def json_route
  route("json", id)
end

#nameObject



33
34
35
# File 'app/models/markdown_record/content_fragment.rb', line 33

def name
  meta[:name] || Pathname.new(id).basename.to_s
end

#parentObject



78
79
80
81
82
83
84
85
86
87
# File 'app/models/markdown_record/content_fragment.rb', line 78

def parent
  if meta[:parent_id].present?
    self.class.find(meta[:parent_id])
  elsif meta[:relative_parent_id].present?
    real_id = Pathname.new(id).parent.join(meta[:relative_parent_id])
    self.class.find(real_id)
  else
    self.class.find(subdirectory)
  end
end

#parents_from(ancestor) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'app/models/markdown_record/content_fragment.rb', line 108

def parents_from(ancestor)
  ancestor = self.class.find(ancestor) if ancestor.is_a?(String)


  parents = []
  current_parent = self

  while current_parent
    if current_parent.id == ancestor&.id
      parents.unshift(current_parent)
      current_parent = nil
    else
      parents.unshift(current_parent)
      current_parent = current_parent.parent
    end
  end

  parents
end

#read_htmlObject



53
54
55
# File 'app/models/markdown_record/content_fragment.rb', line 53

def read_html
  File.read(html_path) if html_exists?
end

#read_jsonObject



49
50
51
# File 'app/models/markdown_record/content_fragment.rb', line 49

def read_json
  File.read(json_path) if json_exists?
end