Module: DataShift::SpreeHelper

Defined in:
lib/datashift_spree.rb,
lib/helpers/spree_helper.rb,
lib/loaders/spree/image_loader.rb,
lib/loaders/spree/product_loader.rb

Defined Under Namespace

Classes: ImageLoader, ProductLoader

Class Method Summary collapse

Class Method Details

.app_rootObject



87
88
89
# File 'lib/helpers/spree_helper.rb', line 87

def self.app_root
  File.join(root, 'app')
end

.gem_nameObject



52
53
54
# File 'lib/datashift_spree.rb', line 52

def self.gem_name
  "datashift_spree"
end

.gem_versionObject



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/datashift_spree.rb', line 40

def self.gem_version
  unless(@gem_version)
    if(File.exists?(File.join(root_path, 'VERSION') ))
      File.read( File.join(root_path, 'VERSION') ).match(/.*(\d+.\d+.\d+)/)
      @gem_version = $1
    else
      @gem_version = '1.0.0'
    end
  end
  @gem_version
end

.get_image_owner(record) ⇒ Object

Return the right OBJECT to attach Product images to for the callers version of Spree



67
68
69
70
71
72
73
# File 'lib/helpers/spree_helper.rb', line 67

def self.get_image_owner(record)
  if(SpreeHelper::version.to_f > 1) 
   record.is_a?(get_product_class) ? record.master : record     # owner is VARIANT
  else
    record.is_a?(get_product_class) ? record : record.product   # owner is PRODUCT
  end
end

.get_product_classObject



45
46
47
48
49
50
51
# File 'lib/helpers/spree_helper.rb', line 45

def self.get_product_class
  if(is_namespace_version())
    Object.const_get('Spree').const_get('Product')
  else
    Object.const_get('Product')
  end
end

.get_spree_class(x) ⇒ Object

Helpers so we can cope with both pre 1.0 and post 1.0 versions of Spree in same datashift version



37
38
39
40
41
42
43
# File 'lib/helpers/spree_helper.rb', line 37

def self.get_spree_class(x)
  if(is_namespace_version())    
    ModelMapper::class_from_string("Spree::#{x}")
  else
    ModelMapper::class_from_string(x.to_s)
  end
end

.is_namespace_versionObject



79
80
81
# File 'lib/helpers/spree_helper.rb', line 79

def self.is_namespace_version
  SpreeHelper::version.to_f >= 1
end

.lib_rootObject



83
84
85
# File 'lib/helpers/spree_helper.rb', line 83

def self.lib_root
  File.join(root, 'lib')
end

.library_pathObject



60
61
62
# File 'lib/datashift_spree.rb', line 60

def self.library_path
  File.expand_path("#{File.dirname(__FILE__)}/../lib")
end

.loadObject



91
92
93
94
# File 'lib/helpers/spree_helper.rb', line 91

def self.load()
  require 'spree'
  require 'spree_core'
end

.load_commandsObject

Load all the datashift Thor commands and make them available throughout app



112
113
114
115
116
117
118
119
# File 'lib/datashift_spree.rb', line 112

def self.load_commands()
  base = File.join(library_path, 'thor', '**')
  
  Dir["#{base}/*.thor"].each do |f|
    next unless File.file?(f)
    Thor::Util.load_thorfile(f)
  end
end

.load_tasksObject

Load all the datashift rake tasks and make them available throughout app



102
103
104
105
106
107
# File 'lib/datashift_spree.rb', line 102

def self.load_tasks
  # Long parameter lists so ensure rake -T produces nice wide output
  ENV['RAKE_COLUMNS'] = '180'
  base = File.join(root_path, 'tasks', '**')
  Dir["#{base}/*.rake"].sort.each { |ext| load ext }
end

.product_attachment_klazzObject

Return the right CLASS to attach Product images to for the callers version of Spree



56
57
58
59
60
61
62
# File 'lib/helpers/spree_helper.rb', line 56

def self.product_attachment_klazz
  @product_attachment_klazz  ||= if(DataShift::SpreeHelper::version.to_f > 1.0 )
    DataShift::SpreeHelper::get_spree_class('Variant' )
  else
    DataShift::SpreeHelper::get_spree_class('Product' )
  end
end

.require_datashift_spreeObject



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/datashift_spree.rb', line 87

def self.require_datashift_spree
  
  require_libs = %w{ loaders helpers }
  require_libs.each do |base|
    Dir[File.join(library_path, base, '**/*.rb')].each do |rb|
      unless File.directory? rb
        puts rb
        require rb
      end
    end
  end

end

.require_librariesObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/datashift_spree.rb', line 64

def self.require_libraries
  
  loader_libs = %w{ lib  }

  # Base search paths - these will be searched recursively
  loader_paths = []

  loader_libs.each {|l| loader_paths << File.join(root_path(), l) }

  # Define require search paths, any dir in here will be added to LOAD_PATH

  loader_paths.each do |base|
    $:.unshift base  if File.directory?(base)
    Dir[File.join(base, '**', '**')].each do |p|
      if File.directory? p
        $:.unshift p
      end
    end
  end
  

end

.rootObject



31
32
33
# File 'lib/helpers/spree_helper.rb', line 31

def self.root
  Gem.loaded_specs['spree_core'] ? Gem.loaded_specs['spree_core'].full_gem_path  : ""
end

.root_pathObject



56
57
58
# File 'lib/datashift_spree.rb', line 56

def self.root_path
  File.expand_path("#{File.dirname(__FILE__)}/..")
end

.versionObject



75
76
77
# File 'lib/helpers/spree_helper.rb', line 75

def self.version
  Gem.loaded_specs['spree'] ? Gem.loaded_specs['spree'].version.version : "0.0.0"
end