Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
byropig committed Dec 22, 2011
0 parents commit 2a7e844
Show file tree
Hide file tree
Showing 34 changed files with 805 additions and 0 deletions.
23 changes: 23 additions & 0 deletions app/controllers/admin/products_controller.rb
@@ -0,0 +1,23 @@
module Admin
class ProductsController < Admin::BaseController

crudify :product,
:title_attribute => 'name', :xhr_paging => true

def index
if params[:category_id].present?
@category = Category.find(params[:category_id])
@products = Product.by_category(@category.id).order('position ASC').paginate(:page => params[:page])
else
paginate_all_products
end
end

def update_prices
Product.update(params[:products].keys, params[:products].values)
flash[:notice] = 'Prices were successfully updated.'
redirect_to :action => "index"
end

end
end
46 changes: 46 additions & 0 deletions app/controllers/products_controller.rb
@@ -0,0 +1,46 @@
class ProductsController < ApplicationController

before_filter :find_all_products, :only => :index
before_filter :find_page

def index
# you can use meta fields from your model instead (e.g. browser_title)
# by swapping @page for @product in the line below:
present(@page)
end

def show
@product = Product.find(params[:id])


@tagged_products = Product.tagged_with(@product.tag_list, :any => :true).order('position ASC')

# you can use meta fields from your model instead (e.g. browser_title)
# by swapping @page for @product in the line below:
present(@page)
end

protected

def find_all_products

if params[:category_id].present?
@category = Category.find(params[:category_id])
@products = Product.by_category(@category.id).order('position ASC')
@page_title = @category.name
else
if params[:upper].present? and params[:lower].present?
@products = Product.greater_than(params[:lower]).less_than(params[:upper])
@page_title = "By Price"
elsif params[:tag].present?
@products = Product.tagged_with(params[:tag]).order('position ASC')
@page_title = params[:tag].capitalize
end
end
end

def find_page
@page = Page.where(:link_url => "/products").first
end

end
3 changes: 3 additions & 0 deletions app/helpers/products_helper.rb
@@ -0,0 +1,3 @@
module ProductsHelper
include ActsAsTaggableOn::TagsHelper
end
38 changes: 38 additions & 0 deletions app/models/product.rb
@@ -0,0 +1,38 @@
class Product < ActiveRecord::Base

acts_as_indexed :fields => [:name, :description, :summary]

acts_as_taggable_on :tags

validates :name, :presence => true, :uniqueness => true
validate :validate_category

scope :less_than, lambda { |price| where("price < ?", price) }
scope :greater_than, lambda { |price| where("price > ?", price) }

scope :best_sellers, where(:best_seller => true)
scope :featured, where(:featured => true)
scope :new_products, where(:new_product => true)

scope :by_category, lambda { |category_id|
ids = []
Category.find(category_id).children.each do |child|
ids << child.id
end
ids << category_id

joins(:categories).where("categories.id" => ids)
}

belongs_to :image
has_and_belongs_to_many :categories
has_many :line_items

def validate_category
categories.each do |category|
if category.children.present?
errors.add_to_base("Cannot assign product to a category that has sub-categories")
end
end
end
end
43 changes: 43 additions & 0 deletions app/views/admin/products/_actions.html.erb
@@ -0,0 +1,43 @@
<% if Admin::ProductsController.searchable? %>
<ul class='search_list'>
<li class='not_a_link'>
<%= render :partial => "/shared/admin/search",
:locals => {
:url => admin_products_path
} %>
</li>
</ul>
<% end %>

<ul class='collapsible_menu'>
<li class='not_a_link'>
<%= link_to 'Categories', '#' %>
</li>
<% Category.all.each do |category| %>
<li>
<%= link_to category.name, admin_category_products_path(category) %>
</li>
<% end %>
</ul>

<ul>
<li>
<%= link_to t('.create_new'), new_admin_product_path,
:class => "add_icon" %>
</li>
<% if !searching? and Admin::ProductsController.sortable? and Product.count > 1 %>
<li class='not_a_link'>
<%= link_to t('.reorder', :what => "Products"),
admin_products_path,
:id => "reorder_action",
:class => "reorder_icon" %>
<%= link_to t('.reorder_done', :what => "Products"),
admin_products_path,
:id => "reorder_action_done",
:style => "display: none;",
:class => "reorder_icon" %>
</li>
<% end %>
</ul>
115 changes: 115 additions & 0 deletions app/views/admin/products/_form.html.erb
@@ -0,0 +1,115 @@
<%= form_for [:admin, @product] do |f| -%>
<%= render :partial => "/shared/admin/error_messages", :locals => {
:object => @product,
:include_object_name => true
} %>

<div class='field'>
<%= f.label :name -%>
<%= f.text_field :name, :class => 'larger widest' -%>
</div>

<div class='field'>
<%= f.label :description -%>
<%= f.text_field :description, :class => 'larger widest' -%>
</div>

<div class='field'>
<%= f.label :tag_list -%>
<%= f.text_field :tag_list -%>
</div>

<div class='field'>
<div id='page-tabs' class='clearfix ui-tabs ui-widget ui-widget-content ui-corner-all'>
<ul id='page_parts'>
<% [:summary].each_with_index do |part, part_index| %>
<li class='ui-state-default<%= ' ui-state-active' if part_index == 0 %>'>
<%= link_to part.to_s.titleize, "##{part}" %>
</li>
<% end %>
</ul>

<div id='page_part_editors'>
<% [:summary].each do |part| %>
<div class='page_part' id='<%= part %>'>
<%= f.text_area part, :rows => 20, :class => 'wymeditor widest' -%>
</div>
<% end %>
</div>
</div>
</div>

<div class='field'>
<%= f.label :price -%>
<%= f.text_field :price -%>
</div>

<div class='field'>
<%= f.label :image -%>
<%= render :partial => "/shared/admin/image_picker", :locals => {
:f => f,
:field => :image_id,
:image => @product.image,
:toggle_image_display => false
} %>
</div>

<div class="field">
<%= f.label :categories %>
<% for category in Category.find(:all) %>
<% if category.children.present? %>
<div>
<% if @product.categories.include?(category) %>
<%= check_box_tag "product[category_ids][]", category.id, @product.categories.include?(category) %>
<% else %>
<img src="/images/arrow.png" height="13px" width="13px" style="margin:3px; vertical-align:bottom;">
<% end %>
<%= raw category.name %>
</div>
<% category.children.each do |child_category| %>
<div style="margin-left:18px;">
<%= check_box_tag "product[category_ids][]", child_category.id, @product.categories.include?(child_category) %>
<%= child_category.name %>
</div>
<% end %>
<% else %>
<% unless category.parent.present? %>
<div>
<%= check_box_tag "product[category_ids][]", category.id, @product.categories.include?(category) %>
<%= category.name %>
</div>
<% end %>
<% end %>
<% end %>
</div>

<div class='field'>
<%= f.label :best_seller -%>
<%= f.check_box :best_seller -%>
</div>

<div class='field'>
<%= f.label :new_product -%>
<%= f.check_box :new_product -%>
</div>

<div class='field'>
<%= f.label :featured -%>
<%= f.check_box :featured -%>
</div>

<%= render :partial => "/shared/admin/form_actions",
:locals => {
:f => f,
:continue_editing => false,
:delete_title => t('delete', :scope => 'admin.products.product'),
:delete_confirmation => t('message', :scope => 'shared.admin.delete', :title => @product.name)
} %>
<% end -%>
<% content_for :javascripts do %>
<script>
$(document).ready(function(){
page_options.init(false, '', '');
});
</script>
<% end %>
24 changes: 24 additions & 0 deletions app/views/admin/products/_product.html.erb
@@ -0,0 +1,24 @@
<li class='clearfix record <%= cycle("on", "on-hover") %>' id="<%= dom_id(product) -%>">
<span class='title'>
<%= product.name %>
<span class="preview">
<br>
<%= fields_for "products[]", product do |f| %>
<b>Price:</b> <%= f.text_field :price, :size => 5%>
<% end %>

</span>
</span>
<span class='actions'>
<%= link_to refinery_icon_tag("application_go.png"), product_path(product),
:title => t('.view_live_html'),
:target => "_blank" %>
<%= link_to refinery_icon_tag("application_edit.png"), edit_admin_product_path(product),
:title => t('.edit') %>
<%= link_to refinery_icon_tag("delete.png"), admin_product_path(product),
:class => "cancel confirm-delete",
:title => t('.delete'),
:confirm => t('message', :scope => 'shared.admin.delete', :title => product.name),
:method => :delete %>
</span>
</li>
2 changes: 2 additions & 0 deletions app/views/admin/products/_products.html.erb
@@ -0,0 +1,2 @@
<%= will_paginate @products %>
<%= render :partial => "sortable_list" %>
18 changes: 18 additions & 0 deletions app/views/admin/products/_records.html.erb
@@ -0,0 +1,18 @@
<% if searching? %>
<h2><%= t('results_for', :scope => 'shared.admin.search', :query => params[:search]) %></h2>
<% end %>
<% if @products.any? %>
<div class='pagination_container'>
<%= render :partial => 'products' %>
</div>
<% else %>
<p>
<% unless searching? %>
<strong>
<%= t('.no_items_yet') %>
</strong>
<% else %>
<%= t('no_results', :scope => 'shared.admin.search') %>
<% end %>
</p>
<% end %>
13 changes: 13 additions & 0 deletions app/views/admin/products/_sortable_list.html.erb
@@ -0,0 +1,13 @@
<ul id='sortable_list'>
<!-- <%#= form_tag([:admin, @product], :method => 'post') do %> -->
<%= form_tag(update_prices_admin_products_path, {:method => :put}) do %>
<%= render :partial => 'product', :collection => @products %>
<%= submit_tag "Edit Prices" %>
<% end %>
</ul>


<%= render :partial => "/shared/admin/sortable_list",
:locals => {
:continue_reordering => (local_assigns.keys.include?(:continue_reordering)) ? continue_reordering : true
} %>
1 change: 1 addition & 0 deletions app/views/admin/products/edit.html.erb
@@ -0,0 +1 @@
<%= render :partial => "form" %>
12 changes: 12 additions & 0 deletions app/views/admin/products/index.html.erb
@@ -0,0 +1,12 @@
<section id='records'>
<%= render :partial => 'records' %>
</section>
<!-- <aside id='actions'> -->
<nav id='actions' class='multilist'>
<%= render :partial => 'actions' %>
</nav>
<!-- </aside> -->
<%= render :partial => '/shared/admin/make_sortable',
:locals => {
:tree => false
} if !searching? and Admin::ProductsController.sortable? and Product.count > 1 %>
1 change: 1 addition & 0 deletions app/views/admin/products/new.html.erb
@@ -0,0 +1 @@
<%= render :partial => "form" %>
28 changes: 28 additions & 0 deletions app/views/products/index.html.erb
@@ -0,0 +1,28 @@
<% content_for :body_content_title do %>
Catalogue - <%= @page_title %>
<% end %>
<% content_for :body_content_left do %>
<% if @products.blank? %>
<p><i>There are no products loaded for this category yet. Or we're temporarily out of stock - check back soon.</i></p>
<% else %>
<%= content_tag('p', raw(@category.description)) if @category.present? %>
<% @products.each do |product| %>
<div class="pod quarter product omega">
<div class="corner"><img src="/images/corner.png" /></div>
<%= link_to(image_fu(product.image, "200x200#c"), product_path(product)) %>
<hr>
<div class="productname"><%= raw product.name %></div>
<p><i><%= raw product.description %></i></p>
<div class="add_to_cart"><p><%= link_to "Details", product_path(product) %> | <%= link_to(image_tag("/images/cart.png"), add_to_cart_path(product, :category_id => @category)) %></p></div>
</div>
<% end %>
<% end %>
<% end %>
<%= render :partial => "/shared/content_page" %>

0 comments on commit 2a7e844

Please sign in to comment.