Skip to content

Commit

Permalink
Added product facet management
Browse files Browse the repository at this point in the history
  • Loading branch information
Jose Rilla committed Oct 20, 2011
1 parent 53902ef commit da03d8f
Show file tree
Hide file tree
Showing 22 changed files with 1,421 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
.sass-cache/
6 changes: 6 additions & 0 deletions app/controllers/admin/facets_controller.rb
Expand Up @@ -38,5 +38,11 @@ def destroy
@page.destroy
end

def tokens
@facets = Facet.with_globalize.where("title like ?", "%#{params[:q]}%")
respond_to do |format|
format.json { render :json => @facets }
end
end
end
end
7 changes: 7 additions & 0 deletions app/controllers/admin/products_controller.rb
Expand Up @@ -37,5 +37,12 @@ def destroy
@page = Product.find params[:id]
@page.destroy
end

def tokens
@products = Product.with_globalize.where("title like ? and parent_id is null", "%#{params[:q]}%")
respond_to do |format|
format.json { render :json => @products }
end
end
end
end
43 changes: 43 additions & 0 deletions app/controllers/pages_controller.rb
@@ -0,0 +1,43 @@
class PagesController < ApplicationController

# This action is usually accessed with the root path, normally '/'
def home
error_404 unless (@page = Page.where(:link_url => '/').first).present?
end

# This action can be accessed normally, or as nested pages.
# Assuming a page named "mission" that is a child of "about",
# you can access the pages with the following URLs:
#
# GET /pages/about
# GET /about
#
# GET /pages/mission
# GET /about/mission
#
def show

@page = Page.find("#{params[:path]}/#{params[:id]}".split('/').last)

if @page.type == "Facet"
@products = @page.products
@facets = @page.children.empty? ? @page.parent.children : @page.children
#@products.map{ |product| product.facets }.flatten.uniq.reject{ |f| facet_ids.include?(f.id) }
# if @facets.empty?
# @products = Sunspot.search(Product){ with(:facet_ids).all_of [facet_ids.first] }.results
# @facets = @products.map{ |product| product.facets }.flatten.uniq
# end
elsif @page.try(:live?) || (refinery_user? && current_user.authorized_plugins.include?("refinery_pages"))
# if the admin wants this to be a "placeholder" page which goes to its first child, go to that instead.
if @page.skip_to_first_child && (first_live_child = @page.children.order('lft ASC').live.first).present?
redirect_to first_live_child.url and return
elsif @page.link_url.present?
redirect_to @page.link_url and return
end
else
error_404
end

end

end
13 changes: 13 additions & 0 deletions app/models/facet.rb
@@ -1,3 +1,16 @@
class Facet < Page
has_and_belongs_to_many :products, :join_table => "facets_products", :foreign_key => "facet_id"
attr_accessible :product_tokens, :set
attr_reader :product_tokens
scope :age, where(:set => 'age')
scope :kind, where(:set => 'kind')
scope :interest, where(:set => 'interest')
def product_tokens=(ids)
self.product_ids = ids.split(",")
end

def as_json(options={})
{ :id => self.id, :name => self.title }
end

end
8 changes: 8 additions & 0 deletions app/models/product.rb
@@ -1,3 +1,11 @@
class Product < Page
has_and_belongs_to_many :facets, :join_table => "facets_products", :foreign_key => "product_id"
attr_accessible :facet_tokens
attr_reader :facet_tokens
def facet_tokens=(ids)
self.facet_ids = ids.split(",")
end
def as_json(options={})
{ :id => self.id, :name => self.title }
end
end
74 changes: 74 additions & 0 deletions app/views/admin/pages/_form.html.erb
@@ -0,0 +1,74 @@
<% content_for :after_javascript_libraries do %>
<%= javascript_include_tag 'jquery.autoresize' %>
<%= javascript_include_tag 'jquery.textchange.min' %>
<%= javascript_include_tag "jquery.tokeninput.js" %>
<%= javascript_include_tag "products-admin" %>
<% end %>
<% content_for :stylesheets do %>
<%= stylesheet_link_tag "token-input" %>
<%= stylesheet_link_tag "catalog" %>
<% end %>
<% url_opts = action_name == 'edit' ? {:url => admin_page_path(@page.id)} : {} %>
<%= form_for [:admin, @page], url_opts.merge({:as => :page}) do |f| %>
<%= render :partial => "/shared/admin/error_messages",
:locals => {
:object => @page,
:include_object_name => true
} %>
<%= render :partial => "locale_picker",
:locals => {
:current_locale => Thread.current[:globalize_locale]
} if ::Refinery.i18n_enabled? %>

<div class="clearfix"></div>

<div class="field">
<%= f.label :title %>
<%= f.text_field :title, :class => "larger widest" %>
</div>

<%= render :partial => "form_fields_after_title",
:locals => {
:f => f
} %>

<div class='field'>
<%= render :partial => "form_page_parts",
:locals => {
:f => f
} %>
</div>

<%= render :partial => "form_advanced_options",
:locals => {
:f => f
} %>
<%= render :partial => "/shared/admin/form_actions",
:locals => {
:f => f,
:continue_editing => true,
:delete_title => t('delete', :scope => 'admin.pages'),
:delete_confirmation => t('message', :scope => 'shared.admin.delete', :title => @page.title)
} %>
<%= render :partial => "form_new_page_parts",
:locals => {
:f => f
} if RefinerySetting.find_or_set(:new_page_parts, false) %>
<% end %>
<% content_for :javascripts do %>
<script>
$(document).ready(function(){
page_options.init(
<%= RefinerySetting.find_or_set(:new_page_parts, false).to_s %>
, "<%= new_admin_page_part_path %>"
, "<%= admin_page_parts_path %>"
);
});
</script>
<% end %>
4 changes: 4 additions & 0 deletions app/views/admin/pages/_form_fields_after_title.html.erb
@@ -0,0 +1,4 @@
<% if f.object.type == "Facet" and f.object.parent.nil? %>
<%= f.label :set, t('facet_set') %>
<%= f.select :set, options_for_select(RefinerySetting.get(:facet_sets).map{ |set| [t(set), set] }) %>
<% end %>
42 changes: 42 additions & 0 deletions app/views/admin/pages/_page.html.erb
@@ -0,0 +1,42 @@
<li class='clearfix record' id="page_<%= page.id -%>">
<div class='clearfix'>
<% if page.children.present? %>
<span class="icon toggle" title="<%= t('expand_collapse', :scope => 'refinery.admin.pages') %>"></span>
<% else %>
<span class="icon"></span>
<% end %>
<span class='title'>
<%= page.title_with_meta.html_safe %>
<% if ::Refinery.i18n_enabled? and ::Refinery::I18n.frontend_locales.many? and
(locales = page.translations.map(&:locale)).present? %>
<span class='preview'>
<% if page.type=="Facet" && page.set.present? %>
<span class='set'><%= t(page.set) %></span>
<% end %>
<% ([page.translation.try(:locale)] | locales).each do |locale| %>
<%= link_to locale,
edit_admin_page_path(page.id, :switch_locale => locale),
:class => 'locale' %>
<% end %>
</span>
<% end %>
</span>
<span class='actions'>
<%= link_to refinery_icon_tag('application_go.png'), page.url,
:target => "_blank",
:title => t('.view_live_html') %>
<%= link_to refinery_icon_tag('application_edit.png'), edit_admin_page_path(page.id),
:class => 'edit',
:title => t('edit', :scope => 'admin.pages') %>
<%= link_to refinery_icon_tag('delete.png'), eval("admin_#{page.type.present? ? page.type.tableize : 'page'}_path(page.id)"),
:class => "cancel confirm-delete",
:title => t('delete', :scope => 'admin.pages'),
:confirm => t('message', :scope => 'shared.admin.delete', :title => page.title_with_meta.gsub(/\ ?<em>.*<\/em>/, "")),
:remote => true,
:method => :delete if page.deletable? %>
</span>
</div>
<ul class='nested' data-ajax-content="<%= children_admin_page_path(page.id) %>">
<%#= render :partial => 'page', :collection => page.children %>
</ul>
</li>
15 changes: 15 additions & 0 deletions app/views/admin/pages/index.html.erb
@@ -0,0 +1,15 @@
<% content_for :stylesheets do %>
<%= stylesheet_link_tag "catalog" %>
<% end %>
<section id='records' class='tree'>
<% caching = RefinerySetting.find_or_set(:cache_pages_backend, false) %>
<% cache_if(caching, [Refinery.base_cache_key, "pages_backend", Globalize.locale].join('_')) do %>
<%= render :partial => 'records' %>
<% end %>
</section>
<section id='actions'>
<%= render :partial => 'actions' %>
</section>
<%= render :partial => "/shared/admin/make_sortable",
:locals => {:tree => true} if @pages.many? -%>

14 changes: 14 additions & 0 deletions app/views/admin/pages/tabs/_architecture.html.erb
@@ -0,0 +1,14 @@
<div class='wym_skin_refinery page_part architecture' id='page_architecture'>
<div class='wym_area_top'>
</div>
<div class='wym_box field'>
<% if @page.type == "Product" %>
<%= f.label :facet_tokens, t('.facets') %>
<%= f.text_field :facet_tokens, "data-pre" => @page.facets.map(&:as_json).to_json %>
<% elsif @page.type = "Facet" %>
<%= f.label :product_tokens, t('.products') %>
<%= f.text_field :product_tokens, "data-pre" => @page.products.map(&:as_json).to_json %>
<% end %>
</div>

</div>
7 changes: 7 additions & 0 deletions config/locales/en.yml
@@ -1,4 +1,7 @@
en:
age: Age
kind: Kind
interest: Interest
shared:
admin:
image_picker:
Expand All @@ -12,6 +15,10 @@ en:
title: Facets
admin:
pages:
tabs:
architecture:
facets: Facetas
products: Productos
new:
title: Título de la página
parent_page: Página madre
Expand Down
7 changes: 7 additions & 0 deletions config/locales/es.yml
@@ -1,4 +1,7 @@
es:
age: Edad
kind: Tipo
interest: Interés
shared:
admin:
image_picker:
Expand All @@ -12,6 +15,10 @@ es:
title: Facetas
admin:
pages:
tabs:
architecture:
facets: Facetas
products: Productos
new:
page_name: Título de la página
page_name_help: El título que se mostrará en los menús y encabezados del frontend
Expand Down
13 changes: 12 additions & 1 deletion config/routes.rb
Expand Up @@ -6,6 +6,9 @@
collection do
post :update_positions
end
member do
get :children
end
end
end

Expand All @@ -14,6 +17,10 @@
resources :products, :except => :show do
collection do
post :update_positions
get :tokens
end
member do
get :children
end
end
end
Expand All @@ -23,8 +30,12 @@
resources :facets, :except => :show do
collection do
post :update_positions
get :tokens
end
member do
get :children
end
end
end

end
11 changes: 11 additions & 0 deletions db/migrate/4_add_set_to_facets.rb
@@ -0,0 +1,11 @@
class AddSetToFacets < ActiveRecord::Migration
def self.up
add_column :pages, :set, :string
RefinerySetting.set :facet_sets, ['age', 'kind', 'interest'].to_yaml
end

def self.down
remove_column :pages, :set
RefinerySetting.delete :facet_sets
end
end
8 changes: 8 additions & 0 deletions lib/refinerycms-products.rb
Expand Up @@ -22,7 +22,15 @@ class Engine < Rails::Engine
end
end

def self.register(tab)
tab.name = "architecture"
tab.partial = "/admin/pages/tabs/architecture"
end

config.after_initialize do
::Refinery::Pages::Tab.register do |tab|
register tab
end
Refinery::Plugin.register do |plugin|
plugin.name = "products"
plugin.pathname = root
Expand Down

0 comments on commit da03d8f

Please sign in to comment.