Reputation: 5899
I would like to use papaerclip in Rails 3 on a model which is not inherited from ActiveRecord::Base. I do not need the model to be stored as the other models are, only some ActiveModel mixings will be used.
I made something like this:
class ThemeSettings
include ActiveModel::Validations
validate :no_attachement_errors
has_attached_file :logo,
:styles => { :mini => '48x48>', :small => '100x100>', :normal => '240x240>', :large => '600x600>' },
:default_style => :normal,
:url => "/assets/logos/:id/:style/:basename.:extension",
:path => ":rails_root/public/assets/logos/:id/:style/:basename.:extension"
def logo_file_name
..
end
def logo_file_name=(file_name)
..
end
def logo_content_type ..
def logo_content_type=(content_type) ..
def logo_file_size ..
def logo_file_size=(file_size) ..
def logo_updated_at ..
def logo_updated_at=(updated_at) ..
end
Paperclip does not like that: the has_attached_file
method is not mixed in: NoMethodError: undefined method 'has_attached_file' for ThemeSettings:Class
. How can I convince Paperclip to like simple classes? Thanks for your help!
Upvotes: 0
Views: 718
Reputation: 7751
Not tested, but theoretically this should work:
require 'paperclip'
class ThemeSettings
include ActiveModel::Validations
include Paperclip
has_attached_file # ...
# ...
end
require Paperclip and then include the Module to your class.
Upvotes: 2
Reputation: 6005
Much simpler to inherit from ActiveRecord::Base
even when there is no db table behind. That way all other gems will work flawlessly. How about this -
class ThemeSettings < ActiveRecord::Base
def self.columns
@columns ||= [];
end
def self.column(name, sql_type = nil, default = nil, null = true)
columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default,
sql_type.to_s, null)
end
# Override the save method to prevent exceptions.
def save(validate = true)
validate ? valid? : true
end
column :logo_file_name
column :logo_content_type
column :logo_file_size
column :logo_updated_at
# You can override the = methods here.
validate :no_attachement_errors
has_attached_file :logo,
:styles => { :mini => '48x48>', :small => '100x100>', :normal => '240x240>', :large => '600x600>' },
:default_style => :normal,
:url => "/assets/logos/:id/:style/:basename.:extension",
:path => ":rails_root/public/assets/logos/:id/:style/:basename.:extension"
end
Upvotes: 3