Permalink
Browse files

Tags for articles

  • Loading branch information...
1 parent 7333e86 commit c904b565dbc3d07a34074e10b53446b242ad90d3 @nebirhos nebirhos committed Jun 24, 2012
@@ -20,6 +20,7 @@ def new
@admin_editing_language = admin_editing_language
@article.meta_keywords = @cfg.default_page_keywords
@article.meta_description = @cfg.default_page_desc
+ @tags = ''
end
def create
@@ -29,6 +30,8 @@ def create
@article.meta_title = @article.title if @article.meta_title.blank?
#@article.meta_keywords = @cfg.default_page_keywords if @article.meta_keywords.blank?
#@article.meta_description = @cfg.default_page_desc if @article.meta_keywords.blank?
+ @article.tags = params[:tags].split(/\s*,\s*/).collect { |name| Tag.find_or_create_by_name(name) }
+ @tags = @article.tags.collect { |t| t.name }.join(', ')
if @article && @article.save && @article.errors.empty?
redirect_to admin_articles_path
else
@@ -40,6 +43,7 @@ def edit
@categories = Category.all
@admin_editing_language = admin_editing_language
@article = Article.find(params[:id])
+ @tags = @article.tags.collect { |t| t.name }.join(', ')
end
def update
@@ -51,6 +55,8 @@ def update
@article.meta_title = @article.title if @article.meta_title.blank?
#@article.meta_keywords = @cfg.default_page_keywords if @article.meta_keywords.blank?
#@article.meta_description = @cfg.default_page_desc if @article.meta_keywords.blank?
+ @article.tags = params[:tags].split(/\s*,\s*/).collect { |name| Tag.find_or_create_by_name(name) }
+ @tags = @article.tags.collect { |t| t.name }.join(', ')
if @article && @article.save && @article.errors.empty?
#redirect_to admin_articles_path
params[:save_and_close] ? (redirect_to admin_articles_path()) : (render :action => "edit")
@@ -0,0 +1,15 @@
+class Admin::TagsController < Admin::AdminController
+
+ ## CanCan authorization - see Ability model
+ authorize_resource
+
+ def index
+ t = Tag.arel_table
+ @tags = Tag.where t[:name].matches "%#{params[:q]}%"
+
+ respond_to do |wants|
+ wants.js { render :json => @tags.collect { |t| t.name } }
+ end
+ end
+
+end
@@ -2,8 +2,9 @@ class Article < ActiveRecord::Base
has_many :categorizations
has_many :categories, :through => :categorizations
has_many :comments, :as => :commentable, :dependent => :destroy
+ has_and_belongs_to_many :tags
accepts_nested_attributes_for :categorizations
-
+
belongs_to :user
validates_presence_of :title
View
@@ -0,0 +1,5 @@
+class Tag < ActiveRecord::Base
+ has_and_belongs_to_many :articles
+
+ validates :name, :uniqueness => true, :presence => true, :length => {:minimum => 3}
+end
@@ -8,7 +8,7 @@
<div class="cell_10">
<%= f.label :short, t('admin.articles.abstract') %>
<%= f.cktext_area :short %>
-
+
<%= f.label :body, t('admin.articles.body') %>
<%= f.cktext_area :body %>
@@ -42,28 +42,36 @@
<legend>Multimedia</legend>
<%= image_tag article.featured_image.url(:thumb) if article.featured_image.present? %></br>
<%= f.label :featured_image, t('admin.general.featured_image') %>
- <%= f.file_field :featured_image %>
+ <%= f.file_field :featured_image %>
</fieldset>
<fieldset class="group">
<legend>Categories</legend>
- <div id='categories_list' class="input_group">
- <% @categories.map do |category| %>
+ <div id='categories_list' class="input_group">
+ <% @categories.map do |category| %>
<%= render :partial => "/admin/categories/checkbox", :object => category, :as => :category, :locals =>{:article => @article} %>
<% end %>
</div>
<div class="cell_12 b_40">
<%= link_to "Add a new category","#", :id => "new_category_link", :class=>"" %></br>
<div id='new_category_form' style="display:none">
- <span>
+ <span>
<%= text_field(:category, :name) %>
<%= link_to "Save","#", :id => "save_category_link" %>
- </div>
+ </div>
</div>
- </fieldset>
+ </fieldset>
+
+ <fieldset class="group">
+ <legend><%= t('admin.tags.tags') %></legend>
+
+ <%= f.label :tags, t('admin.works.tags') %>
+ <%= text_field_tag :tags, @tags, :class=>"large autocomplete" %>
+ </fieldset>
+
</div>
</div>
- </div>
+ </div>
</div>
<div class="wrap block">
@@ -89,7 +97,7 @@
<%= f.text_area :meta_keywords, {
:title => t("admin.pages.hint_meta_keywords"),
:class => "large"
- } %>
+ } %>
</div>
<div class="cell_8">
<%= f.label 'Meta Description' %>
@@ -103,7 +111,7 @@
</ul>
</div>
-<script type="text/javascript">
+<script type="text/javascript">
$('#new_category_link').click(function() {
$('#new_category_form').show();
});
@@ -118,10 +126,10 @@
});
</script>
-<script type="text/javascript">
+<script type="text/javascript">
var title = new LiveValidation('title');
title.add( Validate.Presence );
-
+
// var realemail = new LiveValidation('realemail');
// realemail.add( Validate.Email );
// var atleast = new LiveValidation('atleast');
@@ -261,6 +261,7 @@ en:
article: Article
all_categories: "All categories"
category: "Articles in category %{name}"
+ tag: "Articles tagged %{name}"
author: "Articles by %{name}"
pages:
pages: Pages
@@ -255,6 +255,7 @@ it:
article: Articolo
all_categories: "Tutte le categorie"
category: "Articoli nella categoria %{name}"
+ tag: "Articoli taggati %{name}"
author: "Articoli di %{name}"
pages:
pages: Pagine
View
@@ -54,6 +54,7 @@
resources :comments do
post 'toggle', :on => :member
end
+ resources :tags, :only => [:index]
end
# Public routes
@@ -0,0 +1,9 @@
+class CreateTags < ActiveRecord::Migration
+ def change
+ create_table :tags do |t|
+ t.string :name
+
+ t.timestamps
+ end
+ end
+end
@@ -0,0 +1,8 @@
+class CreateArticlesTags < ActiveRecord::Migration
+ def change
+ create_table :articles_tags, :id => false do |t|
+ t.references :article
+ t.references :tag
+ end
+ end
+end
@@ -1,6 +1,6 @@
// Railsyard CMS admin panel main js file
$(function() {
-
+
// Accordion for main sidebar navigation
$( "ul#accordion" ).accordion({
collapsible: false,
@@ -9,45 +9,45 @@ $(function() {
autoHeight:false,
icons:false
});
-
+
// Accordion dropdown
$( "ul.dropdown" ).accordion({
collapsible: true,
active:false,
- header: 'li a.has_slide', // this is the element that will be clicked to activate the accordion
+ header: 'li a.has_slide', // this is the element that will be clicked to activate the accordion
autoHeight:false,
event: 'mousedown',
icons:false
});
-
+
// Open and close content boxes
$("a.toggle").click(function(){
$(this).toggleClass("toggle_closed").next().slideToggle("slow");
return false; //Prevent the browser jump to the link anchor
});
-
+
// Datepicker config
$( ".datepicker" ).datepicker({ dateFormat: 'd M yy' }); // http://docs.jquery.com/UI/Datepicker/formatDate
-
+
// Uniform config http://pixelmatrixdesign.com/uniform
//$( "select, input:checkbox, input:radio, input:file").uniform();
-
+
// Tipsy config http://onehackoranother.com/projects/jquery/tipsy
$('[title]').tipsy({
fade: true, // fade tooltips in/out?
fallback: '', // fallback text to use when no tooltip text
gravity: 's', // gravity
opacity: 1, // opacity of tooltip
title: 'title', // attribute/callback containing tooltip text
- trigger: 'hover' // how tooltip is triggered - hover | focus | manual
+ trigger: 'hover' // how tooltip is triggered - hover | focus | manual
});
-
+
$('input[title]').tipsy({
- trigger: 'focus',
+ trigger: 'focus',
offset:'5',
gravity: 'w'
});
-
+
// DataTables config http://www.datatables.net
var tableObj = $('.datatable').dataTable( {
"bJQueryUI": true,
@@ -56,23 +56,70 @@ $(function() {
"aaSorting": [[0,'asc']],
"bAutoWidth": true,
"bInfo": true,
- "sScrollY": "100%",
+ "sScrollY": "100%",
"sScrollX": "100%",
"bScrollCollapse": true,
"sPaginationType": "full_numbers",
"bRetrieve": true,
"aLengthMenu": [[5, 10, 30, -1], [5, 10, 30, "All"]],
"iDisplayLength": 10
});
-
+
$(window).bind('resize', function () {
tableObj.fnAdjustColumnSizing();
});
-
+
// static tables alternating rows
$('table.static tr:even').addClass("even")
-
+
//Back to top link
$().UItoTop({ easingType: 'easeOutQuart' });
-
-});
+
+
+ // Tags autocomplete
+ var autocomplete = function() {
+ var split = function ( val ) {
+ return val.split( /\s*,\s*/ );
+ };
+ var extractLast = function ( term ) {
+ return split( term ).pop();
+ };
+
+ $( ".autocomplete" )
+ // don't navigate away from the field on tab when selecting an item
+ .bind( "keydown", function( event ) {
+ if ( event.keyCode === $.ui.keyCode.TAB &&
+ $( this ).data( "autocomplete" ).menu.active ) {
+ event.preventDefault();
+ }
+ })
+ .autocomplete({
+ source: function( request, response ) {
+ $.getJSON( "/admin/tags/", {
+ q: extractLast( request.term )
+ }, response );
+ },
+ search: function() {
+ // custom minLength
+ var term = extractLast( this.value );
+ },
+ focus: function() {
+ // prevent value inserted on focus
+ return false;
+ },
+ select: function( event, ui ) {
+ console.log( 'this:', this );
+ var terms = split( this.value );
+ // remove the current input
+ terms.pop();
+ // add the selected item
+ terms.push( ui.item.value );
+ // add placeholder to get the comma-and-space at the end
+ terms.push( "" );
+ this.value = terms.join( ", " );
+ return false;
+ }
+ });
+ }();
+
+});

0 comments on commit c904b56

Please sign in to comment.