Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save bpartridge/3a9f9cf77665cb25293c to your computer and use it in GitHub Desktop.

Select an option

Save bpartridge/3a9f9cf77665cb25293c to your computer and use it in GitHub Desktop.

Revisions

  1. bpartridge revised this gist May 8, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion app-slash-admin-slash-active_admin_extensions.rb
    Original file line number Diff line number Diff line change
    @@ -23,7 +23,7 @@ def default_table
    id_column
    resource_class.content_columns.each do |col|
    if col.name.ends_with? "img"
    img_column col.name.to_sym, col.name.sub("img","image")
    img_column col.name.to_sym
    else
    column col.name.to_sym
    end
  2. bpartridge created this gist May 8, 2014.
    74 changes: 74 additions & 0 deletions app-slash-admin-slash-active_admin_extensions.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,74 @@
    # This file extends and overrides parts of the ActiveAdmin DSL and internals
    # in order to provide support for automatically displaying and editing images,
    # which in our infrastructure are stored as URLs whose column names end in "img".
    # Since this file will be reloaded frequently in the development environment,
    # all operations done at load time (class_eval's, etc.) MUST be idempotent.

    ActiveAdmin::Views::TableFor.class_eval do

    def img_column(col_sym=:img, title="Image")
    column title, sortable: false do |obj|
    url = obj.send(col_sym)
    link_to(filepicker_image_tag(url, width: 100, height: 100), url)
    end
    end

    end

    # You can also subclass this and do `index as: MyIndexAsTableSubclass` to get the same functionality
    ActiveAdmin::Views::IndexAsTable.class_eval do
    def default_table
    proc do
    selectable_column
    id_column
    resource_class.content_columns.each do |col|
    if col.name.ends_with? "img"
    img_column col.name.to_sym, col.name.sub("img","image")
    else
    column col.name.to_sym
    end
    end
    default_actions
    end
    end
    end

    ActiveAdmin::Views::AttributesTable.class_eval do
    def content_for(record, attr)
    previous = current_arbre_element.to_s

    if attr.to_s.ends_with? "img"
    value = find_attr_value(record, attr)
    value = link_to(filepicker_image_tag(value, width: 200, height: 200), value)
    else
    value = pretty_format find_attr_value(record, attr)
    end

    value.blank? && previous == current_arbre_element.to_s ? empty_value : value
    end
    end

    class FilepickerForActiveAdminInput
    include Formtastic::Inputs::Base
    def to_html
    input_wrapping do
    label_html <<
    builder.filepicker_field(method, input_html_options)
    end
    end
    end

    ActiveAdmin::FormBuilder.class_eval do

    protected

    def default_input_type(method, options = {})
    if column = column_for(method) and column.type == :string
    if method.to_s.ends_with? "img"
    return :filepicker_for_active_admin
    end
    end
    super
    end

    end
    6 changes: 6 additions & 0 deletions config-slash-initializers-slash-inflections.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    # This will provide human-readable column names across the board, including all the
    # ActiveAdmin pages (which all use pretty_format which uses the Inflector).

    ActiveSupport::Inflector.inflections do |inflect|
    inflect.human /img$/, '\1image'
    end