Skip to content

Commit 1679bcf

Browse files
johngrimesFrancesc Esplugas
authored and
Francesc Esplugas
committed
Adding better support for paperclip to typus_fields_for
Allowing you to specify attachment instead of attachment_file_name in the application.yml configuration Signed-off-by: Francesc Esplugas <[email protected]>
1 parent a9d7d0c commit 1679bcf

File tree

12 files changed

+70
-4
lines changed

12 files changed

+70
-4
lines changed

lib/typus/active_record.rb

+12-1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,17 @@ def typus_fields_for(filter)
7777
when 'position' then attribute_type = :position
7878
end
7979

80+
# Set attribute type to file if accompanied by standard
81+
# paperclip attachment fields with its name
82+
paperclip_fields = ["#{field.to_s}_file_name".to_sym,
83+
"#{field.to_s}_content_type".to_sym,
84+
"#{field.to_s}_file_size".to_sym,
85+
"#{field.to_s}_updated_at".to_sym]
86+
87+
if (model_fields.keys & paperclip_fields).size == paperclip_fields.size
88+
attribute_type = :file
89+
end
90+
8091
# And finally insert the field and the attribute_type
8192
# into the fields_with_type ordered hash.
8293
fields_with_type[field.to_s] = attribute_type
@@ -343,4 +354,4 @@ def owned_by?(user)
343354
end
344355

345356
ActiveRecord::Base.send :include, Typus
346-
ActiveRecord::Base.send :include, Typus::InstanceMethods
357+
ActiveRecord::Base.send :include, Typus::InstanceMethods

test/config/working/application.yml

+12-1
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,22 @@ Post:
7171
application: Blog
7272
export: csv, xml
7373

74+
Picture:
75+
fields:
76+
list: title, created_at
77+
form: title, picture
78+
show: title, picture
79+
order_by:
80+
relationships:
81+
filters:
82+
search: title
83+
application: Blog
84+
7485
CustomUser:
7586
fields:
7687
list: first_name, last_name, email
7788

7889
View:
7990
fields:
8091
list: ip, post
81-
form: ip, post
92+
form: ip, post
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class Admin::PicturesController < Admin::MasterController
2+
end

test/fixtures/app/models/picture.rb

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class Picture < ActiveRecord::Base
2+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
_edit.html.erb
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
_index.html.erb
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
_new.html.erb
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
_show.html.erb
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
_sidebar.html.erb

test/fixtures/pictures.yml

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
first:
2+
id: 1
3+
title: Some picture
4+
picture_file_name: dog.jpg
5+
picture_content_type: image/jpeg
6+
picture_file_size: 175938
7+
picture_updated_at: <%= 3.days.ago.to_s(:db) %>
8+
9+
second:
10+
id: 2
11+
title: Another picture
12+
picture_file_name: cat.jpg
13+
picture_content_type: image/jpeg
14+
picture_file_size: 198475
15+
picture_updated_at: <%= 2.days.ago.to_s(:db) %>

test/lib/active_record_test.rb

+10-1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,15 @@ def test_should_return_typus_fields_for_form_for_typus_user
8989
assert_equal expected_fields.map { |i| i.last }, TypusUser.typus_fields_for(:form).values
9090
end
9191

92+
def test_should_return_typus_fields_for_form_for_picture
93+
expected_fields = [["title", :string],
94+
["picture", :file]]
95+
assert_equal expected_fields.map { |i| i.first }, Picture.typus_fields_for("form").keys
96+
assert_equal expected_fields.map { |i| i.last }, Picture.typus_fields_for("form").values
97+
assert_equal expected_fields.map { |i| i.first }, Picture.typus_fields_for(:form).keys
98+
assert_equal expected_fields.map { |i| i.last }, Picture.typus_fields_for(:form).values
99+
end
100+
92101
def test_should_return_typus_fields_for_a_model_without_configuration
93102
expected_fields = []
94103
klass = Class.new(ActiveRecord::Base)
@@ -360,4 +369,4 @@ def test_should_verify_typus_user_id
360369
assert !TypusUser.typus_user_id?
361370
end
362371

363-
end
372+
end

test/schema.rb

+12-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,17 @@
4444
t.datetime :published_at
4545
t.integer :typus_user_id
4646
end
47+
48+
create_table :pictures, :force => true do |t|
49+
t.string :title
50+
t.string :picture_file_name
51+
t.string :picture_content_type
52+
t.integer :picture_file_size
53+
t.datetime :picture_updated_at
54+
t.datetime :created_at
55+
t.datetime :updated_at
56+
t.integer :typus_user_id
57+
end
4758

4859
create_table :typus_users, :force => true do |t|
4960
t.string :first_name, :default => "", :null => false
@@ -72,4 +83,4 @@
7283
add_index :categories_posts, :category_id
7384
add_index :categories_posts, :post_id
7485

75-
end
86+
end

0 commit comments

Comments
 (0)