ensl.org/app/models/concerns/extra.rb

73 lines
1.7 KiB
Ruby
Raw Normal View History

module Extra
2014-04-13 11:16:51 +00:00
extend ActiveSupport::Concern
2014-04-13 11:16:51 +00:00
CODING_HTML = 0
CODING_BBCODE = 1
CODING_MARKDOWN = 2
included do
2014-04-13 11:16:51 +00:00
def codings
{
CODING_HTML => "Plain HTML",
CODING_BBCODE => "BBCode",
CODING_MARKDOWN => "Markdown"
}
end
2014-04-13 11:16:51 +00:00
def check_params(params, filter)
(params.instance_of?(Array) ? params : params.keys).each do |key|
return false unless filter.include? key.to_sym
end
return true
end
2014-04-13 11:16:51 +00:00
def error_messages
self.errors.full_messages.uniq
end
2014-04-13 11:16:51 +00:00
def bbcode_to_html(text)
Sanitize.clean(text.to_s).bbcode_to_html.gsub(/\n|\r\n/, "<br>").html_safe
2014-04-13 11:16:51 +00:00
end
2020-03-29 23:16:34 +00:00
def move_up(objects, column = "position")
2014-04-13 11:16:51 +00:00
n = 0
2020-03-29 23:16:34 +00:00
# the objects need to be assigned before loop or the order is not right
(objects = objects.order(column)).each_with_index do |item, i|
2014-04-13 11:16:51 +00:00
if item.id == id and n > 0
2015-04-09 13:49:35 +00:00
old_position = item[column]
2020-03-29 23:16:34 +00:00
item.update_attribute(column, objects[i-1][column])
objects[i-1].update_attribute(column, old_position)
2014-04-13 11:16:51 +00:00
end
n = n + 1
end
end
2020-03-29 23:16:34 +00:00
def move_down(objects, column = "position")
2014-04-13 11:16:51 +00:00
n = 0
2020-03-29 23:16:34 +00:00
(objects = objects.order(column)).each_with_index do |item, i|
2014-04-13 11:16:51 +00:00
if item.id == id and n < (objects.length-1)
2015-04-09 13:49:35 +00:00
old_position = item[column]
2020-03-29 23:16:34 +00:00
item.update_attribute(column, objects[n+1][column])
objects[n+1].update_attribute(column, old_position)
2014-04-13 11:16:51 +00:00
end
n = n + 1
end
end
2014-04-13 11:16:51 +00:00
def can_show? cuser
true
end
2014-04-13 11:16:51 +00:00
def can_create? cuser
true
end
2014-04-13 11:16:51 +00:00
def can_update? cuser
true
end
2014-04-13 11:16:51 +00:00
def can_destroy? cuser
true
end
end
end