Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Tagging system with autocomplete feature #77

Merged
merged 2 commits into from
Feb 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ gem 'jdbc-mysql', '5.1.28', :platform => :jruby
# gem 'flying-sphinx', '1.2.0'
# Named scopes for PostgreSQL's full text search
gem 'pg_search'

#
gem 'acts-as-taggable-on'
# Implementing autocomplete
gem "selectize-rails"
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.1.8'

Expand Down
7 changes: 6 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ GEM
minitest (~> 5.1)
thread_safe (~> 0.1)
tzinfo (~> 1.1)
acts-as-taggable-on (4.0.0)
activerecord (>= 4.0)
addressable (2.4.0)
ambry (1.0.0)
arel (5.0.1.20140414130214)
Expand Down Expand Up @@ -228,6 +230,7 @@ GEM
sdoc (0.4.1)
json (~> 1.7, >= 1.7.7)
rdoc (~> 4.0)
selectize-rails (0.12.4)
simplecov (0.12.0)
docile (~> 1.1.0)
json (>= 1.8, < 3)
Expand Down Expand Up @@ -275,6 +278,7 @@ PLATFORMS
ruby

DEPENDENCIES
acts-as-taggable-on
bootstrap-sass (~> 3.3.6)
cancancan (~> 1.10)
capistrano (~> 3.2.0)
Expand Down Expand Up @@ -305,6 +309,7 @@ DEPENDENCIES
rubocop
sass-rails (>= 3.2)
sdoc (~> 0.4.0)
selectize-rails
simplecov
spring
truncate_html (~> 0.9.3)
Expand All @@ -315,4 +320,4 @@ DEPENDENCIES
wysiwyg-rails

BUNDLED WITH
1.13.7
1.14.2
1 change: 1 addition & 0 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
// about supported directives.
//
//= require jquery
//= require selectize
//= require jquery.turbolinks
//= require jquery_ujs
//= require turbolinks
Expand Down
2 changes: 2 additions & 0 deletions app/assets/stylesheets/application.css.scss
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
* file per style scope.
*
*= require_self
*= require selectize
*= require selectize.default
*= require froala_editor.min.css
*= require froala_style.min.css
*= require plugins/char_counter.min.css
Expand Down
13 changes: 9 additions & 4 deletions app/controllers/new_threads_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ def index
end

def list
@new_threads = NewThread.paginate(page: params[:page], per_page: 10)
.order('id DESC')
if params[:tag]
@new_threads = NewThread.tagged_with(params[:tag])
else
@new_threads = NewThread.paginate(page: params[:page], per_page: 10)
.order('id DESC')
end
end

def search
Expand All @@ -28,7 +32,7 @@ def show
# the request path will not match the new_thread_path, and we should do
# a 301 redirect that uses the current friendly id.
if request.path != new_thread_path(@new_thread)
return redirect_to @new_thread, status: :moved_permanently
redirect_to @new_thread, status: :moved_permanently
end
end

Expand Down Expand Up @@ -107,7 +111,8 @@ def destroy
# Never trust parameters from the scary internet,
# only allow the white list through.
def new_thread_params
params.require(:new_thread).permit(:title, :description, :user_id)
params.require(:new_thread).permit(:title, :description, :user_id,
:tag_list)
end

def friendly_url
Expand Down
2 changes: 2 additions & 0 deletions app/models/new_thread.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class NewThread < ActiveRecord::Base
acts_as_taggable # Alias for acts_as_taggable_on :tags

extend FriendlyId
friendly_id :title, use: [:slugged, :history]

Expand Down
21 changes: 20 additions & 1 deletion app/views/new_threads/_form2.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,29 @@
<div class='form-group'>
<div class='col-xs-12'><%= f.text_area :description, rows: '20', class: 'form-control', id: 'edit' %></div>
</div>


<div class="form-group">
<%= f.label :tag_list, "Tags (separated by commas)" , class: 'col-xs-2 control-label'%>
<div class='col-xs-11'><%= f.text_field :tag_list, id: 'tags'%></div>
</div>

<%= f.hidden_field :user_id, :value => current_user.id %>

<div class='form-group'>
<div class='col-xs-3'><%= f.submit (action_name == 'new' ? 'Post' : 'Update'), class: 'btn btn-primary btn-lg' %></div>
</div>
<% end %>

<script>
$('#tags').selectize({
plugins: ['remove_button'],
delimiter: ',',
persist: false,
create: function(input) {
return {
value: input,
text: input
}
}
});
</script>
5 changes: 5 additions & 0 deletions app/views/new_threads/list.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
<p><%= truncate_html(new_thread.description.html_safe) %> (<%= link_to "Read more", new_thread, :class => "thread-link"%>)</p>
</div>

<div class="thread-foot">
<span class="glyphicon glyphicon-tags"></span>
<%=new_thread.tag_list.map {|t| link_to t,tag_path(t)}.join(', ') %>
</div>

<div class="thread-foot">
<div class="thread-info">
<%= image_tag new_thread.user.image_url(:thumb).to_s, :class => "img-circle thread-user-image" %>
Expand Down
5 changes: 3 additions & 2 deletions app/views/new_threads/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
</div>

<div class="thread-foot">
<span class="glyphicon glyphicon-tags"></span>
<%= @new_thread.tag_list.map {|t| link_to t,tag_path(t)}.join(', ') %>
</div>

<% if user_signed_in? %>
<% if @new_thread.user.id == current_user.id %>
Expand Down Expand Up @@ -53,8 +56,6 @@
<% end %>
</div>

</div>

</div>
</div>

Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

get 'sitemap.xml', :to => 'sitemap#index', :defaults => { :format => 'xml' }


get 'tags/:tag', to: 'new_threads#index', as: :tag

resources :blockeds

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# This migration comes from acts_as_taggable_on_engine (originally 1)
class ActsAsTaggableOnMigration < ActiveRecord::Migration
def self.up
create_table :tags do |t|
t.string :name
end

create_table :taggings do |t|
t.references :tag

# You should make sure that the column created is
# long enough to store the required class names.
t.references :taggable, polymorphic: true
t.references :tagger, polymorphic: true

# Limit is created to prevent MySQL error on index
# length for MyISAM table type: http://bit.ly/vgW2Ql
t.string :context, limit: 128

t.datetime :created_at
end

add_index :taggings, :tag_id
add_index :taggings, [:taggable_id, :taggable_type, :context]
end

def self.down
drop_table :taggings
drop_table :tags
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# This migration comes from acts_as_taggable_on_engine (originally 2)
class AddMissingUniqueIndices < ActiveRecord::Migration
def self.up
add_index :tags, :name, unique: true

remove_index :taggings, :tag_id if index_exists?(:taggings, :tag_id)
remove_index :taggings, [:taggable_id, :taggable_type, :context]
add_index :taggings,
[:tag_id, :taggable_id, :taggable_type, :context, :tagger_id,
:tagger_type], unique: true, name: 'taggings_idx'
end

def self.down
remove_index :tags, :name

remove_index :taggings, name: 'taggings_idx'

add_index :taggings, :tag_id unless index_exists?(:taggings, :tag_id)
add_index :taggings, [:taggable_id, :taggable_type, :context]
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# This migration comes from acts_as_taggable_on_engine (originally 3)
class AddTaggingsCounterCacheToTags < ActiveRecord::Migration
def self.up
add_column :tags, :taggings_count, :integer, default: 0

ActsAsTaggableOn::Tag.reset_column_information
ActsAsTaggableOn::Tag.find_each do |tag|
ActsAsTaggableOn::Tag.reset_counters(tag.id, :taggings)
end
end

def self.down
remove_column :tags, :taggings_count
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# This migration comes from acts_as_taggable_on_engine (originally 4)
class AddMissingTaggableIndex < ActiveRecord::Migration
def self.up
add_index :taggings, [:taggable_id, :taggable_type, :context]
end

def self.down
remove_index :taggings, [:taggable_id, :taggable_type, :context]
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# This migration comes from acts_as_taggable_on_engine (originally 5)
# This migration is added to circumvent issue #623 and have special characters
# work properly
class ChangeCollationForTagNames < ActiveRecord::Migration
def up
if ActsAsTaggableOn::Utils.using_mysql?
execute('ALTER TABLE tags MODIFY name varchar(255) CHARACTER
SET utf8 COLLATE utf8_bin;')
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# This migration comes from acts_as_taggable_on_engine (originally 6)
class AddMissingIndexes < ActiveRecord::Migration
def change
add_index :taggings, :tag_id
add_index :taggings, :taggable_id
add_index :taggings, :taggable_type
add_index :taggings, :tagger_id
add_index :taggings, :context

add_index :taggings, [:tagger_id, :tagger_type]
add_index :taggings, [:taggable_id, :taggable_type, :tagger_id,
:context], name: 'taggings_idy'
end
end
29 changes: 28 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20150530031708) do
ActiveRecord::Schema.define(version: 20170127162442) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -183,6 +183,33 @@
t.datetime "updated_at"
end

create_table "taggings", force: true do |t|
t.integer "tag_id"
t.integer "taggable_id"
t.string "taggable_type"
t.integer "tagger_id"
t.string "tagger_type"
t.string "context", limit: 128
t.datetime "created_at"
end

add_index "taggings", ["context"], name: "index_taggings_on_context", using: :btree
add_index "taggings", ["tag_id", "taggable_id", "taggable_type", "context", "tagger_id", "tagger_type"], name: "taggings_idx", unique: true, using: :btree
add_index "taggings", ["tag_id"], name: "index_taggings_on_tag_id", using: :btree
add_index "taggings", ["taggable_id", "taggable_type", "context"], name: "index_taggings_on_taggable_id_and_taggable_type_and_context", using: :btree
add_index "taggings", ["taggable_id", "taggable_type", "tagger_id", "context"], name: "taggings_idy", using: :btree
add_index "taggings", ["taggable_id"], name: "index_taggings_on_taggable_id", using: :btree
add_index "taggings", ["taggable_type"], name: "index_taggings_on_taggable_type", using: :btree
add_index "taggings", ["tagger_id", "tagger_type"], name: "index_taggings_on_tagger_id_and_tagger_type", using: :btree
add_index "taggings", ["tagger_id"], name: "index_taggings_on_tagger_id", using: :btree

create_table "tags", force: true do |t|
t.string "name"
t.integer "taggings_count", default: 0
end

add_index "tags", ["name"], name: "index_tags_on_name", unique: true, using: :btree

create_table "users", force: true do |t|
t.string "username", limit: 25
t.string "email", default: "", null: false
Expand Down