Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcin Naglik committed May 12, 2009
1 parent eaab53c commit bd9db25
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 0 deletions.
11 changes: 11 additions & 0 deletions init.rb
@@ -0,0 +1,11 @@
ActiveScaffold.bridge "Paperclip" do
install do
if ActiveScaffold::Config::Core.instance_methods.include?("initialize_with_paperclip")
raise RuntimeError, "We've detected that you have active_scaffold_paperclip_bridge installed. This plugin has been moved to core. Please remove active_scaffold_paperclip_bridge to prevent any conflicts"
end

require File.join(File.dirname(__FILE__), "lib/as_paperclip_bridge")
require File.join(File.dirname(__FILE__), "lib/form_ui")
require File.join(File.dirname(__FILE__), "lib/list_ui")
end
end
46 changes: 46 additions & 0 deletions lib/as_paperclip_bridge.rb
@@ -0,0 +1,46 @@
module ActiveScaffold::Config
class Core < Base

def initialize_with_paperclip(model_id)
initialize_without_paperclip(model_id)
return unless PaperclipHelpers.has_paperclip_fields(self.model)

self.model.send :extend, PaperclipHelpers

self.update.multipart = true
self.create.multipart = true

self.model.paperclip_fields.each{ |field|
configure_paperclip_field(field.to_sym)
}
end

alias_method_chain :initialize, :paperclip

def configure_paperclip_field(field)
self.columns << field
self.columns[field].list_ui ||= self.model.attachment_definitions[:data][:styles].include?(:thumb) ? :paperclip_thumb : :paperclip_link
self.columns[field].form_ui ||= :paperclip

['file_name', 'content_type', 'file_size', 'updated_at'].each{ |f|
self.columns.exclude("#{field}_#{f}".to_sym)
}
end
end
end

module PaperclipHelpers
class << self
def paperclip_fields(klass)
klass.attachment_definitions.nil? ? [] : klass.attachment_definitions.keys
end

def has_paperclip_fields(klass)
paperclip_fields(klass).size > 0 if paperclip_fields(klass)
end
end

def paperclip_fields
@paperclip_fields||=PaperclipHelpers.paperclip_fields(self)
end
end
25 changes: 25 additions & 0 deletions lib/form_ui.rb
@@ -0,0 +1,25 @@
module ActiveScaffold
module Helpers
module FormColumnHelpers
def active_scaffold_input_paperclip(column, options)
if @record.send("#{column.name}_file_name")
if @record.send(column.name).styles.include?(:thumb)
content_tag(
:div,
content_tag(
:div,
link_to(image_tag(@record.send(column.name).url(:thumb), :border => 0), @record.send(column.name).url, :popup => true) + " " +
hidden_field(:record, "delete_#{column.name}", :value => "false")
)
)
else
content_tag(:p, link_to(@record.send("#{column.name}_file_name"), @record.send(column.name).url, :popup => true), :class => "text-input")+" "+
hidden_field(:record, "delete_#{column.name}", :value => "false")
end
else
file_field(:record, column.name, options)+" "+"<input type=hidden name='record[photos][-1][delete]'>"
end
end
end
end
end
21 changes: 21 additions & 0 deletions lib/list_ui.rb
@@ -0,0 +1,21 @@
module ActiveScaffold
module Helpers
module ListColumnHelpers

def active_scaffold_column_paperclip_link(column, record)
return nil if record.send(column.name).nil?
link_to(
record.send("#{column.name}_file_name"),
record.send(column.name).url,
:popup => true)
end

def active_scaffold_column_paperclip_thumb(column, record)
link_to(
image_tag(record.send(column.name).url(:thumb), :border => 0),
record.send(column.name).url,
:popup => true)
end
end
end
end
29 changes: 29 additions & 0 deletions test/test.rb
@@ -0,0 +1,29 @@
require 'test/unit'
require "rubygems"
require 'active_record'
require
require 'active_support'

class MyModel < ActiveRecord::Base
has_attached_file :data,
:styles => {
:thumb => "50x50#",
:large => "640x480#"
}

validates_attachment_presence :data
validates_attachment_content_type :data,
:content_type => ['image/jpeg', 'image/pjpeg',
'image/jpg', 'image/png']
end

class PaperclipColumnTest < Test::Unit::TestCase
def setup
@model = MyModel.new
end

def test__sth

end
end

0 comments on commit bd9db25

Please sign in to comment.