Skip to content

Commit bd9db25

Browse files
author
Marcin Naglik
committed
first commit
1 parent eaab53c commit bd9db25

File tree

5 files changed

+132
-0
lines changed

5 files changed

+132
-0
lines changed

init.rb

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
ActiveScaffold.bridge "Paperclip" do
2+
install do
3+
if ActiveScaffold::Config::Core.instance_methods.include?("initialize_with_paperclip")
4+
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"
5+
end
6+
7+
require File.join(File.dirname(__FILE__), "lib/as_paperclip_bridge")
8+
require File.join(File.dirname(__FILE__), "lib/form_ui")
9+
require File.join(File.dirname(__FILE__), "lib/list_ui")
10+
end
11+
end

lib/as_paperclip_bridge.rb

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
module ActiveScaffold::Config
2+
class Core < Base
3+
4+
def initialize_with_paperclip(model_id)
5+
initialize_without_paperclip(model_id)
6+
return unless PaperclipHelpers.has_paperclip_fields(self.model)
7+
8+
self.model.send :extend, PaperclipHelpers
9+
10+
self.update.multipart = true
11+
self.create.multipart = true
12+
13+
self.model.paperclip_fields.each{ |field|
14+
configure_paperclip_field(field.to_sym)
15+
}
16+
end
17+
18+
alias_method_chain :initialize, :paperclip
19+
20+
def configure_paperclip_field(field)
21+
self.columns << field
22+
self.columns[field].list_ui ||= self.model.attachment_definitions[:data][:styles].include?(:thumb) ? :paperclip_thumb : :paperclip_link
23+
self.columns[field].form_ui ||= :paperclip
24+
25+
['file_name', 'content_type', 'file_size', 'updated_at'].each{ |f|
26+
self.columns.exclude("#{field}_#{f}".to_sym)
27+
}
28+
end
29+
end
30+
end
31+
32+
module PaperclipHelpers
33+
class << self
34+
def paperclip_fields(klass)
35+
klass.attachment_definitions.nil? ? [] : klass.attachment_definitions.keys
36+
end
37+
38+
def has_paperclip_fields(klass)
39+
paperclip_fields(klass).size > 0 if paperclip_fields(klass)
40+
end
41+
end
42+
43+
def paperclip_fields
44+
@paperclip_fields||=PaperclipHelpers.paperclip_fields(self)
45+
end
46+
end

lib/form_ui.rb

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module ActiveScaffold
2+
module Helpers
3+
module FormColumnHelpers
4+
def active_scaffold_input_paperclip(column, options)
5+
if @record.send("#{column.name}_file_name")
6+
if @record.send(column.name).styles.include?(:thumb)
7+
content_tag(
8+
:div,
9+
content_tag(
10+
:div,
11+
link_to(image_tag(@record.send(column.name).url(:thumb), :border => 0), @record.send(column.name).url, :popup => true) + " " +
12+
hidden_field(:record, "delete_#{column.name}", :value => "false")
13+
)
14+
)
15+
else
16+
content_tag(:p, link_to(@record.send("#{column.name}_file_name"), @record.send(column.name).url, :popup => true), :class => "text-input")+" "+
17+
hidden_field(:record, "delete_#{column.name}", :value => "false")
18+
end
19+
else
20+
file_field(:record, column.name, options)+" "+"<input type=hidden name='record[photos][-1][delete]'>"
21+
end
22+
end
23+
end
24+
end
25+
end

lib/list_ui.rb

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module ActiveScaffold
2+
module Helpers
3+
module ListColumnHelpers
4+
5+
def active_scaffold_column_paperclip_link(column, record)
6+
return nil if record.send(column.name).nil?
7+
link_to(
8+
record.send("#{column.name}_file_name"),
9+
record.send(column.name).url,
10+
:popup => true)
11+
end
12+
13+
def active_scaffold_column_paperclip_thumb(column, record)
14+
link_to(
15+
image_tag(record.send(column.name).url(:thumb), :border => 0),
16+
record.send(column.name).url,
17+
:popup => true)
18+
end
19+
end
20+
end
21+
end

test/test.rb

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
require 'test/unit'
2+
require "rubygems"
3+
require 'active_record'
4+
require
5+
require 'active_support'
6+
7+
class MyModel < ActiveRecord::Base
8+
has_attached_file :data,
9+
:styles => {
10+
:thumb => "50x50#",
11+
:large => "640x480#"
12+
}
13+
14+
validates_attachment_presence :data
15+
validates_attachment_content_type :data,
16+
:content_type => ['image/jpeg', 'image/pjpeg',
17+
'image/jpg', 'image/png']
18+
end
19+
20+
class PaperclipColumnTest < Test::Unit::TestCase
21+
def setup
22+
@model = MyModel.new
23+
end
24+
25+
def test__sth
26+
27+
end
28+
end
29+

0 commit comments

Comments
 (0)