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



288
289
290
291
292
293
294
# File 'lib/muck-contents/models/content.rb', line 288

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



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

def auto_translate
  begin
    translate(false)
  rescue => ex
    #TODO figure out a way to bubble up the error
    puts ex
    # 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)


279
280
281
282
283
284
285
# File 'lib/muck-contents/models/content.rb', line 279

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



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

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



231
232
233
# File 'lib/muck-contents/models/content.rb', line 231

def current_editor=(editor)
  @current_editor = editor
end

#disallow_edit(user) ⇒ Object

Remove permissions from a specific user



297
298
299
# File 'lib/muck-contents/models/content.rb', line 297

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

#ensure_locale_is_stringObject



250
251
252
# File 'lib/muck-contents/models/content.rb', line 250

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

#get_content_scopeObject

Setup the scope for this content object



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

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



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

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



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

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.



241
242
243
244
245
246
247
248
# File 'lib/muck-contents/models/content.rb', line 241

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/



268
269
270
271
272
273
# File 'lib/muck-contents/models/content.rb', line 268

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



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

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.



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

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.



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

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

#translate(overwrite_user_edited_translations = false) ⇒ Object

Translate title and body using Google



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

def translate(overwrite_user_edited_translations = false)
  title_translations = Babelphish::Translator.multiple_translate(self.title, Babelphish::GoogleTranslate::LANGUAGES, self.locale)
  body_translations = Babelphish::Translator.multiple_translate(self.body, Babelphish::GoogleTranslate::LANGUAGES, self.locale)
  existing_translations = {}
  self.content_translations.each do |translation|
    existing_translations[translation.locale] = translation
  end
  
  Babelphish::GoogleTranslate::LANGUAGES.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



225
226
227
# File 'lib/muck-contents/models/content.rb', line 225

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



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

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



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

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)


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

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