Module: Database

Defined in:
lib/generators/liquid_cms/templates/vendor/plugins/liquid/performance/shopify/database.rb

Class Method Summary collapse

Class Method Details

.tablesObject

Load the standard vision toolkit database and re-arrage it to be simply exportable to liquid as assigns. All this is based on Shopify



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/generators/liquid_cms/templates/vendor/plugins/liquid/performance/shopify/database.rb', line 6

def self.tables
  @tables ||= begin      
    db = YAML.load_file(File.dirname(__FILE__) + '/vision.database.yml')

    # From vision source      
    db['products'].each do |product|
      collections = db['collections'].find_all do |collection|
        collection['products'].any? { |p| p['id'].to_i == product['id'].to_i }
      end      
      product['collections'] = collections      
    end

    # key the tables by handles, as this is how liquid expects it.      
    db = db.inject({}) do |assigns, (key, values)|
      assigns[key] = values.inject({}) { |h, v| h[v['handle']] = v; h; }
      assigns
    end  
    
    # Some standard direct accessors so that the specialized templates 
    # render correctly
    db['collection'] = db['collections'].values.first
    db['product']    = db['products'].values.first
    db['blog']       = db['blogs'].values.first
    db['article']    = db['blog']['articles'].first
          
    db['cart']       = { 
      'total_price' => db['line_items'].values.inject(0) { |sum, item| sum += item['line_price'] * item['quantity'] },
      'item_count'  => db['line_items'].values.inject(0) { |sum, item| sum += item['quantity'] },
      'items'       => db['line_items'].values
    }
          
    db
  end    
end