Commit 457ec7a051c46c642a8f886a08f725d63c56d108

SourceImportation model
  
1010 accepts_nested_attributes_for :uri
1111 belongs_to :container, :polymorphic => true
1212
13 has_many :source_importations, :dependent => :destroy
14
1315 validates_presence_of :uri, :target
1416 validates_presence_of :content_type, :on => :update
1517
3333 end
3434
3535 def import
36 target_class = target.to_s.constantize
37
3836 feed.entries.each do |entry|
37 # Import feed entries until reach an already imported one
3938 break if imported_at && entry.updated < imported_at
4039
41 target_entry = target_class.new.respond_to?(:guid) &&
42 entry.id.present? &&
43 import_class.find_by_guid(entry.id) ||
44 import_class.new
45
46 target_entry.from_atom!(entry)
40 # Find previous Importation with this guid
41 if entry.id.present? && old_source_importation = source_importations.find_by_guid(entry.id)
42 # Importation may have been deleted previously
43 if old_source_importation.importation.present?
44 old_source_importation.importation.from_atom!(entry)
45 end
46 old_source_importation.touch
47 else
48 # Create new Importation
49 source_importations.create :importation => importation_class.new.from_atom!(entry),
50 :guid => entry.id
51 end
4752 end
4853
4954 update_attribute :imported_at, Time.now
114114 end
115115 end
116116
117 def import_class
117 # The target class to be instanciated when importing from from this source
118 def importation_class
118119 self.container && self.container.send(target.tableize) ||
119120 target.constantize
120121 end
  
1class SourceImportation < ActiveRecord::Base
2 belongs_to :source
3 belongs_to :importation, :polymorphic => true, :dependent => :destroy
4end
  
6868 cattr_reader :per_page
6969 class_variable_set "@@per_page", options[:per_page]
7070
71 if SourceImportation.table_exists?
72 has_one :source_importation, :as => :importation, :dependent => :destroy
73 end
74
7175 acts_as_sortable
7276
7377 extend ClassMethods
166166
167167 self.attributes = self.class.params_from_atom(entry)
168168
169 if respond_to?(:guid)
170 self.guid = entry.id
171 end
172
173169 if respond_to?(:created_at)
174170 self.created_at = entry.published
175171 end
178178 # TODO: find by OpenID or other attributes
179179 self.author = Anonymous.current
180180 end
181
182 self
181183 end
182184
183185 # Update attributes using params_from_atom and save the Resource
184186 #
185187 # Saves the record without timestamps, so created_at and updated_at are the original from the feed entry
186188 def from_atom!(entry)
187 class_timestamps_cache = self.class.record_timestamps
188 self.class.record_timestamps = false
189 from_atom(entry)
190 save!
191 self.class.record_timestamps = class_timestamps_cache
189 freezing_timestamps do
190 from_atom(entry)
191 save!
192 end
193
194 self
192195 end
193196
194197 # The arguments to polymorphic_path to build the path for this resource Logo
212212 respond_to?(:attachment_options) &&
213213 attachment_options[:thumbnails].keys.map(&:to_s).include?(options[:size].to_s) &&
214214 thumbnails.find_by_thumbnail(options[:size].to_s).present?
215 end
216
217 def freezing_timestamps
218 class_timestamps_cache = self.class.record_timestamps
219 self.class.record_timestamps = false
220 yield
221 self.class.record_timestamps = class_timestamps_cache
215222 end
216223 end
217224 end