Module: MuckContents::Models::MuckContent

Extended by:
ActiveSupport::Concern
Defined in:
lib/muck-contents/models/content.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#allow_edit(user) ⇒ Object

Give permissions to a specific user to edit this content



292
293
294
295
296
297
298
# File 'lib/muck-contents/models/content.rb', line 292

def allow_edit(user)
  permission = self.content_permissions.by_user(user).first
  if !permission # Make sure the user is only added once
    permission = self.content_permissions.create(:user => user)
  end
  permission
end

#auto_translateObject

Called after ‘save’ if auto translate is enabled



189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/muck-contents/models/content.rb', line 189

def auto_translate
  return unless MuckContents.configuration.enable_auto_translations
  begin
    translate(false)
  rescue => ex
    #TODO figure out a way to bubble up the error
    puts ex
    debugger
    # Translations failed, but update the default language
    translation = translation_for(self.locale)
    translation.update_attributes!(:title => self.title, :body => self.body) unless translation.blank?
  end
end

#can_edit?(user) ⇒ Boolean

Override this method to change the way edit permissions are handled on contents By default the creator or a user in the roles ‘editor’, ‘manager’ or ‘admin’ can edit the object If the content is owned by some object such as a group or project then you might change this method to let members of the group or project edit the content.

Returns:

  • (Boolean)


283
284
285
286
287
288
289
# File 'lib/muck-contents/models/content.rb', line 283

def can_edit?(user)
  return true if check_creator(user)
  return true if user.any_role?('editor', 'manager')
  return true if !self.content_permissions.by_user(user).blank?
  return true if self.parent && self.parent.can_add_content?(user)
  false
end

#current_editorObject

Get the user that is currently editing the content



240
241
242
# File 'lib/muck-contents/models/content.rb', line 240

def current_editor
  @current_editor || creator
end

#current_editor=(editor) ⇒ Object

Set the user who is currently editing the content. This is used to determine permissions



235
236
237
# File 'lib/muck-contents/models/content.rb', line 235

def current_editor=(editor)
  @current_editor = editor
end

#disallow_edit(user) ⇒ Object

Remove permissions from a specific user



301
302
303
# File 'lib/muck-contents/models/content.rb', line 301

def disallow_edit(user)
  self.content_permissions.by_user(user).destroy_all
end

#ensure_locale_is_stringObject



254
255
256
# File 'lib/muck-contents/models/content.rb', line 254

def ensure_locale_is_string
  self.locale = self.locale.to_s
end

#get_content_scopeObject

Setup the scope for this content object



150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/muck-contents/models/content.rb', line 150

def get_content_scope
  if !self.custom_scope.blank?
    File.join('/', self.custom_scope) # make sure the scope starts with a '/'
  elsif !self.contentable.blank?
    self.class.contentable_to_scope(self.contentable)
  else
    if self.uri_path
      self.uri_path
    else
      MuckContents::GLOBAL_SCOPE
    end
  end
end

#locale_body(current_locale) ⇒ Object

Returns a body specific to the provided locale



174
175
176
177
178
179
180
# File 'lib/muck-contents/models/content.rb', line 174

def locale_body(current_locale)
  if self.locale != current_locale
    setup_locale(current_locale)
    return @locale_contents[current_locale].body if @locale_contents[current_locale]
  end
  self.body
end

#locale_title(current_locale) ⇒ Object

Returns a title specific to the provided locale



165
166
167
168
169
170
171
# File 'lib/muck-contents/models/content.rb', line 165

def locale_title(current_locale)
  if self.locale != current_locale
    setup_locale(current_locale)
    return @locale_contents[current_locale].title if @locale_contents[current_locale]
  end
  self.title
end

#sanitize_attributesObject

Sanitize content before saving. This prevent XSS attacks and other malicious html.



245
246
247
248
249
250
251
252
# File 'lib/muck-contents/models/content.rb', line 245

def sanitize_attributes
  if self.sanitize_level
    self.body = Sanitize.clean(self.body_raw, self.sanitize_level)
    self.title = Sanitize.clean(self.title, self.sanitize_level)
  else
    self.body = self.body_raw
  end
end

#sanitize_levelObject

Override this method to control sanitization levels. Currently a user who is an admin will not have their content sanitized. A user in any role ‘editor’, ‘manager’, or ‘contributor’ will be given the ‘RELAXED’ settings while all other users will get ‘BASIC’.

By default the ‘creator’ of the content will be used to determine which level of sanitization is allowed. To change this set ‘current_editor’ before

Options are from sanitze: nil - no sanitize Sanitize::Config::RELAXED Sanitize::Config::BASIC Sanitize::Config::RESTRICTED for more details see: rgrove.github.com/sanitize/



272
273
274
275
276
277
# File 'lib/muck-contents/models/content.rb', line 272

def sanitize_level
  return Sanitize::Config::BASIC if current_editor.nil?
  return nil if current_editor.admin?
  return Sanitize::Config::RELAXED if current_editor.any_role?('editor', 'manager', 'contributor')
  Sanitize::Config::BASIC
end

#scopeObject

get scope from the slug



132
133
134
# File 'lib/muck-contents/models/content.rb', line 132

def scope
  self.slug.scope
end

#search_contentObject

Provided for solr index. Override this method if you wish to add other fields/data to the solr index.



184
185
186
# File 'lib/muck-contents/models/content.rb', line 184

def search_content
  "#{body}"
end

#setup_uri_pathObject

uri_path is used to calculate scope on save and therefore must be recovered before a save is executed. Use this method to set it from the uri method.



127
128
129
# File 'lib/muck-contents/models/content.rb', line 127

def setup_uri_path
  self.uri_path = self.class.scope_from_uri(self.uri)
end

#translate(overwrite_user_edited_translations = false, translate_to = nil) ⇒ Object

Translate title and body using Google



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/muck-contents/models/content.rb', line 204

def translate(overwrite_user_edited_translations = false, translate_to = nil)
  translate_to ||= MuckContents.configuration.translate_to
  return if translate_to.blank?
  
  title_translations = Babelphish::Translator.multiple_translate(self.title, translate_to, self.locale)
  body_translations = Babelphish::Translator.multiple_translate(self.body, translate_to, self.locale)
  existing_translations = {}
  self.content_translations.each do |translation|
    existing_translations[translation.locale] = translation
  end
  
  translate_to.each do |language|
    if translation = existing_translations[language]
      if !translation.user_edited || overwrite_user_edited_translations
        translation.update_attributes!(:title => title_translations[language],
                                       :body => body_translations[language])
      end
    else
      self.content_translations.create!(:title => title_translations[language],
                                        :body => body_translations[language],
                                        :locale => language)
    end
  end
end

#translation_for(locale) ⇒ Object



229
230
231
# File 'lib/muck-contents/models/content.rb', line 229

def translation_for(locale)
  self.content_translations.by_locale(locale).first
end

#uriObject

The model must be saved before uri becomes valid. It is calculated using the model’s scope and id



121
122
123
# File 'lib/muck-contents/models/content.rb', line 121

def uri
  File.join(self.scope, self.to_param)
end

#uri=(val) ⇒ Object

Uri that will identify this content on the website Splits up a uri into a path part and a key part that will automatically be assigned to the title. For example: given /faq/widgets/the_green_one will assign uri_path = faq/widgets and return the key the_green_one



111
112
113
114
115
116
117
# File 'lib/muck-contents/models/content.rb', line 111

def uri=(val)
  self.title = self.class.id_from_uri(val)
  if self.title
    self.title = self.title.titleize 
  end
  self.uri_path = self.class.scope_from_uri(val)
end

#valid_uri?Boolean

TODO for some reason if valid? fails even before save an exception is being thrown. Uncomment this method if you figure out why. if contentable is blank then a uri that identifies this content must be specified def valid?

if !valid_uri?
  errors.add_to_base(I18n.t('muck.contents.no_uri_error'))
end

end

Returns:

  • (Boolean)


145
146
147
# File 'lib/muck-contents/models/content.rb', line 145

def valid_uri?
  !self.contentable.blank? || !self.uri_path.blank?
end