Class: ComfortableMexicanSofa::Fixture::Importer

Inherits:
Object
  • Object
show all
Defined in:
lib/comfortable_mexican_sofa/fixture.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(from, to = from, force_import = false) ⇒ Importer

Returns a new instance of Importer.



11
12
13
14
15
16
17
18
19
20
# File 'lib/comfortable_mexican_sofa/fixture.rb', line 11

def initialize(from, to = from, force_import = false)
  self.from         = from
  self.to           = to
  self.site         = Comfy::Cms::Site.where(:identifier => to).first!
  self.fixture_ids  = []
  self.force_import = force_import
  
  dir = self.class.name.split('::')[2].downcase.pluralize
  self.path = ::File.join(ComfortableMexicanSofa.config.fixtures_path, from, dir, '/')
end

Instance Attribute Details

#fixture_idsObject

Returns the value of attribute fixture_ids.



4
5
6
# File 'lib/comfortable_mexican_sofa/fixture.rb', line 4

def fixture_ids
  @fixture_ids
end

#force_importObject

Returns the value of attribute force_import.



4
5
6
# File 'lib/comfortable_mexican_sofa/fixture.rb', line 4

def force_import
  @force_import
end

#fromObject

Returns the value of attribute from.



4
5
6
# File 'lib/comfortable_mexican_sofa/fixture.rb', line 4

def from
  @from
end

#pathObject

Returns the value of attribute path.



4
5
6
# File 'lib/comfortable_mexican_sofa/fixture.rb', line 4

def path
  @path
end

#siteObject

Returns the value of attribute site.



4
5
6
# File 'lib/comfortable_mexican_sofa/fixture.rb', line 4

def site
  @site
end

#toObject

Returns the value of attribute to.



4
5
6
# File 'lib/comfortable_mexican_sofa/fixture.rb', line 4

def to
  @to
end

Instance Method Details

#fresh_fixture?(object, file_path) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/comfortable_mexican_sofa/fixture.rb', line 22

def fresh_fixture?(object, file_path)
  object.new_record? || self.force_import || ::File.mtime(file_path) > object.updated_at
end

#get_attributes(file_path) ⇒ Object



26
27
28
# File 'lib/comfortable_mexican_sofa/fixture.rb', line 26

def get_attributes(file_path)
  YAML.load_file(file_path) || { }
end

#import!Object



43
44
45
46
47
48
49
# File 'lib/comfortable_mexican_sofa/fixture.rb', line 43

def import!
  ComfortableMexicanSofa::Fixture::Category::Importer.new(from, to, force_import).import!
  ComfortableMexicanSofa::Fixture::Layout::Importer.new(  from, to, force_import).import!
  ComfortableMexicanSofa::Fixture::Page::Importer.new(    from, to, force_import).import!
  ComfortableMexicanSofa::Fixture::Snippet::Importer.new( from, to, force_import).import!
  ComfortableMexicanSofa::Fixture::File::Importer.new(    from, to, force_import).import!
end

#save_categorizations!(object, categories) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/comfortable_mexican_sofa/fixture.rb', line 30

def save_categorizations!(object, categories)
  object.categorizations.delete_all
  [categories].flatten.compact.each do |label|
    category = self.site.categories.find_or_create_by(
      :label            => label,
      :categorized_type => object.class.to_s
    )
    category.categorizations.create!(
      :categorized => object
    )
  end
end