Module: ImportableAttachments::Base::ClassMethods
- Defined in:
- lib/importable_attachments/base.rb
Instance Method Summary collapse
-
#has_importable_attachment(options) ⇒ Object
:call-seq: has_importable_attachment opts.
- #install_importable_attachment_assignment_protection ⇒ Object
- #install_importable_attachment_associations ⇒ Object
- #install_importable_attachment_options(options) ⇒ Object
- #install_importable_attachment_validations ⇒ Object
- #validate_importable_attachment_options(options) ⇒ Object
Instance Method Details
#has_importable_attachment(options) ⇒ Object
:call-seq: has_importable_attachment opts
opts
:import_method
:import_into
:spreadsheet_columns
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/importable_attachments/base.rb', line 12 def () lopt = {import_method: :import_rows} lopt.merge! lopt lopt include InstanceMethods # for assigning attachment to new record after_create :import_attachment end |
#install_importable_attachment_assignment_protection ⇒ Object
70 71 72 73 74 |
# File 'lib/importable_attachments/base.rb', line 70 def attr_accessible :attachment, :attachment_attributes, :attachment_id accepts_nested_attributes_for :attachment, :allow_destroy => true, :reject_if => :all_blank end |
#install_importable_attachment_associations ⇒ Object
45 46 47 48 49 50 |
# File 'lib/importable_attachments/base.rb', line 45 def has_one :attachment, :dependent => :nullify, :as => :attachable delegate :io_stream, :to => :attachment, :prefix => true delegate :io_stream_url, :to => :attachment, :prefix => true delegate :io_stream_file_name, :to => :attachment, :prefix => true end |
#install_importable_attachment_options(options) ⇒ Object
38 39 40 41 42 43 |
# File 'lib/importable_attachments/base.rb', line 38 def () [:import_method, :import_into, :spreadsheet_columns].each do |sym| cattr_accessor sym self.send("#{sym}=".to_sym, [sym]) end end |
#install_importable_attachment_validations ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/importable_attachments/base.rb', line 52 def validates :attachment, :associated => true validate do |record| if @columns_not_found "column(s) not found: #{@columns_not_found}" end if !@row_errors.blank? "failed to import #{@row_errors.length} record(s)" @row_errors.each {|row| .errors.add(:base, row)} end end # These go in the class calling has_importable_attachment as they are # dependent on mime-type expectations #validates_with CsvValidator, :if => Proc.new {|model| model.attachment.present?} #validates_with ExcelValidator, :if => Proc.new {|model| model.attachment.present?} end |
#validate_importable_attachment_options(options) ⇒ Object
28 29 30 31 32 33 34 35 36 |
# File 'lib/importable_attachments/base.rb', line 28 def () [:spreadsheet_columns, :import_into].each do |sym| raise RuntimeError, "has_importable_attachment: needs :#{sym}" unless .has_key? sym end unless [:spreadsheet_columns].is_a?(Enumerable) raise RuntimeError, 'has_importable_attachment: :spreadsheet_columns must be Enumerable' end end |