Skip to content

Instantly share code, notes, and snippets.

@glaucocustodio
Last active March 20, 2016 08:50
Show Gist options
  • Save glaucocustodio/5985229 to your computer and use it in GitHub Desktop.
Save glaucocustodio/5985229 to your computer and use it in GitHub Desktop.
Rails 4 , Rails Admin and Rich
# Following https://github.com/sferik/rails_admin/wiki/Theming-and-customization I have created a custom js to load rich base, in this way I get the assets included on top of page
# /assets/javascripts/rails_admin/custom/ui.js
//= require rich/base
//= require_self
# File upload at Rails 4
# How rich still uses 'attr_accessible' and I didnt want use attr_protected gem I had to overrode RichFile class to comment attr_accessible, I just created /models/rich/rich_file.rb with the same content from rich gem and commented out attr_acessible line.
# I overrode rich javascripts
# /assets/javascripts/rich/editor/ckeditor_path.js
CKEDITOR_BASEPATH = '/assets/ckeditor/';
# /assets/javascripts/rich/editor/rich_editor.js.erb
// CKEditor plugin configuration
function addQueryString( url, params ) {
var queryString = [];
if (!params) return url;
else {
for (var i in params) queryString.push(i + "=" + encodeURIComponent( params[i]));
}
return url + ( ( url.indexOf( "?" ) != -1 ) ? "&" : "?" ) + queryString.join( "&" );
}
// load external plugins
CKEDITOR.plugins.addExternal('MediaEmbed', '<%= asset_path('/assets/ckeditor-contrib/plugins/MediaEmbed/') %>');
CKEDITOR.plugins.addExternal('richfile', '<%= asset_path('/assets/ckeditor-contrib/plugins/richfile/') %>');
# Finally I overrode views responsible to display rich text in rails_admin
# /views/rails_admin/main/_form_rich_picker.html.haml
/= javascript_include_tag "rich/base.js"
= form.send field.view_helper, field.method_name, field.html_attributes.merge({:style=> (if field.editor_options[:hidden_input]==true then "visibility:hidden;position:absolute" else "" end )})
= link_to t('picker_browse').capitalize, Rich.editor[:richBrowserUrl], :class => "btn create"
- unless field.editor_options[:type]==:file
%img{:src => field.preview_image_path, :style => "padding: 10px 0; display: block; max-width: #{field.editor_options[:preview_size]}; max-width: #{field.editor_options[:preview_size]};", :class => 'rich-image-preview'}
= javascript_tag do
$(function(){$('##{form.dom_id(field)}_field a').click(function(e){ e.preventDefault(); assetPicker.showFinder('##{form.dom_id(field)}', #{field.editor_options.to_json.html_safe})})})
# /views/rails_admin/main/_form_rich_text.html.haml
= form.send 'text_area', field.method_name, field.html_attributes.reverse_merge((hdv = field.html_default_value).nil? ? {} : { :value => hdv })
/= javascript_include_tag "rich/base.js"
:javascript
var instance = CKEDITOR.instances['#{form.dom_id(field)}'];
if(instance) { CKEDITOR.remove(instance); }
CKEDITOR.replace('#{form.dom_id(field)}', #{Rich.options(field.config, field.scope_type, field.scope_id).to_json.html_safe});
# Don't forget to re-compile your assets or simply delete the content of your tmp/cache folder
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment