Skip to content

Instantly share code, notes, and snippets.

@vitaliel
Created March 30, 2012 13:54
Show Gist options
  • Save vitaliel/2251705 to your computer and use it in GitHub Desktop.
Save vitaliel/2251705 to your computer and use it in GitHub Desktop.
module AdminHelper
def field_must_be_slug(element, separator = '-')
render :update do |page|
page << <<-rawJS
var element = $('#{element.to_s}');
new Form.Element.Observer(element, 0.02, function(){
element.value = element.getValue().toLowerCase().gsub(/[^a-z0-9\\d\\s\\s+\\#{separator}]+/, '').gsub(/\\s+/,'#{separator}');
});
rawJS
end
end
def auto_url_info(object, *args)
options = args.shift || {}
options[:slug_preparsing] ||= true
options[:url_preview_id] ||= :url_preview
initial_url = object.url.link(:full, "http://localhost:3000")
slug_source_id = [object.class.name.underscore, "url", "attributes", "slug"].join("_")
result = Array.new
result << content_tag(:span, nil, :class => 'icon link')
result << content_tag(:span, initial_url, :id => :url_preview)
result << field_must_be_slug(slug_source_id) if options[:slug_preparsing]
result << <<-rawJS
<script type="javascript">
var makeNewUrl = function(){
var newUrl = new Template('#{object.url.link(:js_template, @site, :object => object)}');
var currentSlug = $('#{slug_source_id}').value.toSlug();
var currentObjectName = '#{object.class.class_name.downcase}';
if(currentSlug.length > 0 && currentObjectName != 'page') currentSlug = '/' + currentSlug;
var showUrl = {
slug: currentSlug
};
xtu = newUrl.evaluate(showUrl).split('://');
newUrl = [xtu[0], xtu[1].gsub(/\\/+/, '/')].join('://');
$('#{options[:url_preview_id].to_s}').update(newUrl);
}
new Form.Element.Observer('#{slug_source_id}', 0.02, makeNewUrl);
makeNewUrl();
</script>
rawJS
end
return result.join
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment