Sha256: f00f5e70a7e49cceccf7b1931f7d11a808b3801973732d6f74e446c13c90392f

Contents?: true

Size: 1.59 KB

Versions: 32

Compression:

Stored size: 1.59 KB

Contents

require 'yaml'
module Database
  
  # Load the standard vision toolkit database and re-arrage it to be simply exportable 
  # to liquid as assigns. All this is based on Shopify
  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
end

if __FILE__ == $0
  p Database.tables['collections']['frontpage'].keys
  #p Database.tables['blog']['articles']
end

Version data entries

32 entries across 32 versions & 3 rubygems

Version Path
liquor-0.1.1 performance/shopify/database.rb
liquor-0.1.0 performance/shopify/database.rb
liquid_cms-0.3.2.0 lib/generators/liquid_cms/templates/vendor/plugins/liquid/performance/shopify/database.rb
liquid_cms-0.2.2.0 generators/liquid_cms/templates/vendor/plugins/cms_plugins/liquid/performance/shopify/database.rb
liquid_cms-0.2.1.1 generators/liquid_cms/templates/vendor/plugins/cms_plugins/liquid/performance/shopify/database.rb
liquid_cms-0.3.1.0 lib/generators/liquid_cms/templates/vendor/plugins/liquid/performance/shopify/database.rb
liquid_cms-0.2.1.0 generators/liquid_cms/templates/vendor/plugins/cms_plugins/liquid/performance/shopify/database.rb
liquid_cms-0.2.0.13 generators/liquid_cms/templates/vendor/plugins/cms_plugins/liquid/performance/shopify/database.rb
liquid_cms-0.3.0.10 lib/generators/liquid_cms/templates/vendor/plugins/liquid/performance/shopify/database.rb
liquid_cms-0.3.0.9 lib/generators/liquid_cms/templates/vendor/plugins/liquid/performance/shopify/database.rb
liquid_cms-0.2.0.12 generators/liquid_cms/templates/vendor/plugins/cms_plugins/liquid/performance/shopify/database.rb
liquid_cms-0.2.0.11 generators/liquid_cms/templates/vendor/plugins/cms_plugins/liquid/performance/shopify/database.rb
liquid_cms-0.2.0.10 generators/liquid_cms/templates/vendor/plugins/cms_plugins/liquid/performance/shopify/database.rb
liquid_cms-0.3.0.8 lib/generators/liquid_cms/templates/vendor/plugins/liquid/performance/shopify/database.rb
liquid_cms-0.2.0.9 generators/liquid_cms/templates/vendor/plugins/cms_plugins/liquid/performance/shopify/database.rb
liquid_cms-0.3.0.7 lib/generators/liquid_cms/templates/vendor/plugins/liquid/performance/shopify/database.rb
liquid_cms-0.3.0.6 lib/generators/liquid_cms/templates/vendor/plugins/liquid/performance/shopify/database.rb
liquid_cms-0.2.0.8 generators/liquid_cms/templates/vendor/plugins/cms_plugins/liquid/performance/shopify/database.rb
liquid_cms-0.3.0.5 lib/generators/liquid_cms/templates/vendor/plugins/liquid/performance/shopify/database.rb
liquid_cms-0.2.0.7 generators/liquid_cms/templates/vendor/plugins/cms_plugins/liquid/performance/shopify/database.rb