Simmo
Simmo

Reputation: 1729

How do I add an additional field to the ckeditor_assets table when using the ckeditor rails gem?

According to the README for the CKEditor gem, I should be able to set an alternative scope for images and attachments. I tried to do this and it simply translates this to a field in the ckeditor_assets table.

I can create a migration to add the required field but how do I configure ckeditor (presumably via the autogenerated models) so that the field is populated with the right data when a new record is created?

Upvotes: 1

Views: 976

Answers (2)

wacaw
wacaw

Reputation: 196

In my opinion the simplest solution:

#controllers/application_controller.rb
protected

def ckeditor_pictures_scope(options = { :assetable_id => "#{current_page.id}" ,:assetable_type => "Page" })
  ckeditor_filebrowser_scope(options)
end

def ckeditor_attachment_files_scope(options = { :assetable_id => "#{current_page.id}" ,:assetable_type => "Page" })
  ckeditor_filebrowser_scope(options)
end


def ckeditor_before_create_asset(asset)
  asset.assetable = current_page if current_page
  return true
end

Upvotes: 0

Simmo
Simmo

Reputation: 1729

Solved.

I used a before_save filter to set the field as I wanted it. Then the methods are altered in application controller to use the field as a scope.

# models/ckeditor/asset.rb

before_save :set_company_id

def set_company_id
  self.company_id = assetable.try(:company_id)
end


# controllers/application_controller.rb

protected

def ckeditor_pictures_scope(options = { :company_id => "#{company_id}" })
  ckeditor_filebrowser_scope(options)
end

def ckeditor_attachment_files_scope(options = { :company_id => "#{company_id}" })
  ckeditor_filebrowser_scope(options)
end

Upvotes: 2

Related Questions