Class: Sections::Model::SectionEntry

Inherits:
Sequel::Model show all
Includes:
Zen::Model::Helper
Defined in:
lib/zen/package/sections/lib/sections/model/section_entry.rb

Overview

Model that represents a singe section entry.

Since:

Constant Summary

Constant Summary

Constants included from Zen::Model::Helper

Zen::Model::Helper::NoRegexpSupport

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from Zen::Model::Helper

included

Methods inherited from Sequel::Model

pk_hash

Instance Attribute Details

- (Object) fields

A hash that will contain all the custom fields and the values of these fields for a single entry.

Since:

  • 0.1



36
37
38
# File 'lib/zen/package/sections/lib/sections/model/section_entry.rb', line 36

def fields
  @fields
end

Class Method Details

+ (Mixed) search(query)

Searches for a set of section entries based on the specified search query.

Parameters:

  • query (String)

    The search query.

Returns:

  • (Mixed)

Since:

  • 16-10-2011



46
47
48
# File 'lib/zen/package/sections/lib/sections/model/section_entry.rb', line 46

def self.search(query)
  return filter(search_column(:title, query))
end

+ (Hash) status_hash

Returns a hash containing all the entry statuses. The keys of this hash are the IDs and the values the names.

Returns:

  • (Hash)

Since:

  • 0.2.8



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/zen/package/sections/lib/sections/model/section_entry.rb', line 76

def self.status_hash
  ::Zen::Language.load('section_entries')

  statuses = {}

  ::Sections::Model::SectionEntryStatus.all.each do |status|
    statuses[status.id] = lang(
      "section_entries.special.status_hash.#{status.name}"
    )
  end

  return statuses
end

Instance Method Details

- (Object) before_save

Hook that is executed before saving an existing section entry.

Since:

  • 0.2.8



217
218
219
220
221
222
223
224
225
# File 'lib/zen/package/sections/lib/sections/model/section_entry.rb', line 217

def before_save
  if self.section_entry_status_id.nil?
    self.section_entry_status_id = ::Sections::Model::SectionEntryStatus[
      :name => 'draft'
    ].id
  end

  super
end

- (Array) custom_fields

Retrieves all custom fields for a section entry.

Returns:

  • (Array)

Since:

  • 0.2.8



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/zen/package/sections/lib/sections/model/section_entry.rb', line 96

def custom_fields
  return CustomFields::Model::CustomField.select_all(:custom_fields) \
    .join(
      :custom_field_groups,
      :custom_field_groups__id => :custom_fields__custom_field_group_id
    ) \
    .join(
      :custom_field_groups_sections,
      :custom_field_groups_sections__custom_field_group_id \
        => :custom_field_groups__id
    ) \
    .filter(
      :custom_field_groups_sections__section_id => section_id
    ) \
    .all
end

- (Hash) custom_fields_hash

Gathers all the custom field groups, custom fields and custom field values and returns them as a hash. This hash can be used in views to build the HTML for all the fields.

Returns:

  • (Hash)

Since:

  • 0.2.8



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/zen/package/sections/lib/sections/model/section_entry.rb', line 164

def custom_fields_hash
  result    = {}
  values    = {}
  groups    = section.custom_field_groups
  group_ids = groups.map { |g| g.id }
  fields    = {}

  # Get all the custom fields in one go rather than running a query for
  # each group.
  CustomFields::Model::CustomField.filter(
    :custom_field_group_id => group_ids
  ).each do |field|
    fields[field.custom_field_group_id] ||= []
    fields[field.custom_field_group_id].push(field)
  end

  # Index the custom field values hash so that the keys are the IDs of the
  # custom fields and the values the instances of
  # CustomFields::Model::CustomFieldValue.
  custom_field_values.each do |val|
    values[val.custom_field_id] = val
  end

  # Build the hash containing all the details of each field
  groups.each do |group|
    result[group.id] ||= {:name => group.name, :fields => []}

    # Don't process the fields if there aren't any to begin with.
    next unless fields.key?(group.id)

    fields[group.id].each do |field|
      m = field.custom_field_type.custom_field_method.name

      begin
        result[group.id][:fields].push(
          CustomFields::BlueFormParameters.send(
            m, field, values[field.id]
          )
        )
      rescue => e
        Ramaze::Log.error(e)
      end
    end
  end

  return result
end

- (Hash) possible_categories

Retrieves all the possible categories for an entry and returns these as a hash. This hash is a multi dimensional hash where the keys are the names of all available category groups and the values hashes with the IDs and names of all the categories for that group.

Examples:

e = Sections::Model::SectionEntry[1]
e.possible_categories # => {'Blog' => {1 => 'General', 2 => 'Code'}}

Returns:

  • (Hash)

Since:

  • 0.2.8



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/zen/package/sections/lib/sections/model/section_entry.rb', line 126

def possible_categories
  hash  = {}
  query = Zen.database[:category_groups_sections] \
    .select(
      :categories__id,
      :categories__name,
      :category_groups__name => :category_group_name
    ) \
    .left_join(
      :categories,
      :category_groups_sections__category_group_id \
        => :categories__category_group_id
    ) \
    .left_join(
      :category_groups,
      :category_groups_sections__category_group_id => :category_groups__id
    ) \
    .filter(
      :category_groups_sections__section_id => section_id
    ) \
    .all

  query.each do |row|
    hash[row[:category_group_name]]           ||= {}
    hash[row[:category_group_name]][row[:id]]   = row[:name]
  end

  return hash
end

- (Object) validate

Specify our validation rules.

Since:

  • 0.1



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/zen/package/sections/lib/sections/model/section_entry.rb', line 55

def validate
  validates_presence([:title, :user_id])
  validates_integer(:section_entry_status_id)

  validates_max_length(255, [:title, :slug])

  # Check if the slug is unique for the current section
  if !SectionEntry \
  .filter({:slug => slug, :section_id => section_id}, ~{:id => id}) \
  .all.empty?
    errors.add(:slug, lang('zen_models.unique'))
  end
end