Skip to content

Instantly share code, notes, and snippets.

@celeryclub
Last active December 20, 2015 03:19
Show Gist options
  • Save celeryclub/6062958 to your computer and use it in GitHub Desktop.
Save celeryclub/6062958 to your computer and use it in GitHub Desktop.
Example RailsAdmin configuration with a nested resource.
# app/models/image.rb
class Image < ActiveRecord::Base
belongs_to :term
def serializable_hash(options = nil)
hash = super
hash.delete_if { |k, v| v.blank? }
hash.delete('filename')
hash['file_path'] = self.filename.present? ? self.filename.url.split('/').last(2).join('/') : nil
hash.except('created_at', 'updated_at')
end
end
# config/initializers/rails_admin.rb
RailsAdmin.config do |config|
### snip
config.audit_with :history, 'User'
### snip
end
# app/models/term.rb
class Term < ActiveRecord::Base
has_many :images, :order => 'preferred DESC', :dependent => :destroy
accepts_nested_attributes_for :images, :allow_destroy => true
def serializable_hash(options = nil)
hash = super
hash['title'] ||= "#{self.note_2} #{self.note_1}"
hash.delete('audio')
hash['audio_path'] = self.audio.present? ? self.audio.url.split('/').last(2).join('/') : nil
hash.except('slug', 'created_at', 'updated_at', 'audio_notes', 'image_notes', 'note_1', 'note_2')
end
end
# app/models/user.rb
class User < ActiveRecord::Base
devise :database_authenticatable,
:recoverable, :rememberable, :trackable, :validatable
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment