Commit 457ec7a051c46c642a8f886a08f725d63c56d108
- Diff rendering mode:
- inline
- side by side
app/models/source.rb
(17 / 9)
|   | |||
| 10 | 10 | accepts_nested_attributes_for :uri | |
| 11 | 11 | belongs_to :container, :polymorphic => true | |
| 12 | 12 | ||
| 13 | has_many :source_importations, :dependent => :destroy | ||
| 14 | |||
| 13 | 15 | validates_presence_of :uri, :target | |
| 14 | 16 | validates_presence_of :content_type, :on => :update | |
| 15 | 17 | ||
| … | … | ||
| 33 | 33 | end | |
| 34 | 34 | ||
| 35 | 35 | def import | |
| 36 | target_class = target.to_s.constantize | ||
| 37 | |||
| 38 | 36 | feed.entries.each do |entry| | |
| 37 | # Import feed entries until reach an already imported one | ||
| 39 | 38 | break if imported_at && entry.updated < imported_at | |
| 40 | 39 | ||
| 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 | ||
| 47 | 52 | end | |
| 48 | 53 | ||
| 49 | 54 | update_attribute :imported_at, Time.now | |
| … | … | ||
| 114 | 114 | end | |
| 115 | 115 | end | |
| 116 | 116 | ||
| 117 | def import_class | ||
| 117 | # The target class to be instanciated when importing from from this source | ||
| 118 | def importation_class | ||
| 118 | 119 | self.container && self.container.send(target.tableize) || | |
| 119 | 120 | target.constantize | |
| 120 | 121 | end |
|   | |||
| 1 | class SourceImportation < ActiveRecord::Base | ||
| 2 | belongs_to :source | ||
| 3 | belongs_to :importation, :polymorphic => true, :dependent => :destroy | ||
| 4 | end |
lib/active_record/resource.rb
(19 / 9)
|   | |||
| 68 | 68 | cattr_reader :per_page | |
| 69 | 69 | class_variable_set "@@per_page", options[:per_page] | |
| 70 | 70 | ||
| 71 | if SourceImportation.table_exists? | ||
| 72 | has_one :source_importation, :as => :importation, :dependent => :destroy | ||
| 73 | end | ||
| 74 | |||
| 71 | 75 | acts_as_sortable | |
| 72 | 76 | ||
| 73 | 77 | extend ClassMethods | |
| … | … | ||
| 166 | 166 | ||
| 167 | 167 | self.attributes = self.class.params_from_atom(entry) | |
| 168 | 168 | ||
| 169 | if respond_to?(:guid) | ||
| 170 | self.guid = entry.id | ||
| 171 | end | ||
| 172 | |||
| 173 | 169 | if respond_to?(:created_at) | |
| 174 | 170 | self.created_at = entry.published | |
| 175 | 171 | end | |
| … | … | ||
| 178 | 178 | # TODO: find by OpenID or other attributes | |
| 179 | 179 | self.author = Anonymous.current | |
| 180 | 180 | end | |
| 181 | |||
| 182 | self | ||
| 181 | 183 | end | |
| 182 | 184 | ||
| 183 | 185 | # Update attributes using params_from_atom and save the Resource | |
| 184 | 186 | # | |
| 185 | 187 | # Saves the record without timestamps, so created_at and updated_at are the original from the feed entry | |
| 186 | 188 | 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 | ||
| 192 | 195 | end | |
| 193 | 196 | ||
| 194 | 197 | # The arguments to polymorphic_path to build the path for this resource Logo | |
| … | … | ||
| 212 | 212 | respond_to?(:attachment_options) && | |
| 213 | 213 | attachment_options[:thumbnails].keys.map(&:to_s).include?(options[:size].to_s) && | |
| 214 | 214 | 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 | ||
| 215 | 222 | end | |
| 216 | 223 | end | |
| 217 | 224 | end |

