Class: Blacksand::Page

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.query_value(name, value) ⇒ Object



147
148
149
# File 'app/models/blacksand/page.rb', line 147

def self.query_value(name, value)
  joins(properties: :field).where(fields: {name: name}, properties: {value: value})
end

.selectNode(nodes, page_id) ⇒ Object



135
136
137
138
139
140
141
142
143
144
# File 'app/models/blacksand/page.rb', line 135

def self.selectNode(nodes, page_id)
  nodes.each do |node|
    if node[:page_id] == page_id
      node.merge!(state: {selected: true})
      return
    end

    selectNode(node[:nodes], page_id) if node[:nodes].present?
  end
end

.tree_nodes(page_ids = nil) ⇒ Object



127
128
129
130
131
132
133
# File 'app/models/blacksand/page.rb', line 127

def self.tree_nodes(page_ids = nil)
  if page_ids
    Page.where(id: page_ids).order(:position).map { |p| p.tree_node }
  else
    Page.where('parent_id is null').order(:position).map { |p| p.tree_node }
  end
end

Instance Method Details

#all_image_srcs_of_contentObject



84
85
86
87
88
# File 'app/models/blacksand/page.rb', line 84

def all_image_srcs_of_content
  html_doc = Nokogiri::HTML(self.content)
  images = html_doc.xpath("//img")
  images.map{|img| img['src'] }
end

#ancestorsObject



51
52
53
54
55
56
57
# File 'app/models/blacksand/page.rb', line 51

def ancestors
  if self.parent.present?
    self.parent.ancestors + [self.parent]
  else
    []
  end
end

#build_propertiesObject



121
122
123
124
125
# File 'app/models/blacksand/page.rb', line 121

def build_properties
  self.prototype.fields.each do |field|
    Property.build_property(self, field)
  end
end

#child(title) ⇒ Object



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

def child(title)
  self.children.where(title: title).first
end

#child_with(conditions) ⇒ Object



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

def child_with(conditions)
  self.children.find_by(conditions)
end

#content_first_imageObject



75
76
77
78
79
80
81
82
# File 'app/models/blacksand/page.rb', line 75

def content_first_image
  image_assets = image_assets_of_content

  return if image_assets.empty?

  asset_name = image_assets.first
  Kindeditor::Asset.where(asset: asset_name).first
end

#descendentsObject



59
60
61
62
63
64
65
# File 'app/models/blacksand/page.rb', line 59

def descendents
  self.positioned_children.reduce([]) do |all, child|
    all << child
    all.concat(child.descendents)
    all
  end
end

#preferred_child_prototype_nameObject



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

def preferred_child_prototype_name
  preferred_option('preferred_child_prototype_name')
end

#preferred_child_template_nameObject



90
91
92
# File 'app/models/blacksand/page.rb', line 90

def preferred_child_template_name
  preferred_option('preferred_child_template_name')
end

#propsObject

page.props.name page.props



30
31
32
33
34
35
36
37
# File 'app/models/blacksand/page.rb', line 30

def props
  if @attrs.blank?
    properties = self.properties.includes(:field).map { |p| [p.field.name, p.content] }
    @attrs = OpenStruct.new(Hash[properties])
  end

  @attrs
end

#tree_nodeObject



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'app/models/blacksand/page.rb', line 98

def tree_node
  href = Blacksand::Engine.routes.url_helpers.children_partial_pages_path(parent_id: self.id)

  return {text: self.title, href: href, page_id: self.id} if self.children.count == 0

  # children 多余 15 个且都是叶子节点, 那么子节点不显示
  if self.children.count >= 15 && self.children.limit(15).all? { |p| p.children.empty? }
    {
    text: self.title,
    href: href,
    page_id: self.id
    }
  else
    {
    text: self.title,
    href: href,
    page_id: self.id,
    nodes: self.children.order(:position).map { |p| p.tree_node },
    }
  end

end

#valuesObject

推荐使用 props 方法 @gaohui 2015.11.26



40
41
42
43
44
45
46
47
48
49
# File 'app/models/blacksand/page.rb', line 40

def values
  if @values.blank?
    @values = ActiveSupport::HashWithIndifferentAccess.new
    self.properties.each do |p|
      @values[p.field.name] = p.content
    end
  end

  @values
end