-
-
Save serradura/7baed9eece2d29c194fa01180a29107f to your computer and use it in GitHub Desktop.
Example: Use Acts-As-Taggable-On gem with Active-Admin and rails observers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ActiveAdmin.register Product do | |
permit_params :name, :category_id, :description, :text, :slug, :in_slider, :tag_ids => [] | |
filter :category | |
filter :base_tags | |
filter :name | |
filter :text | |
filter :created_at | |
filter :updated_at | |
controller do | |
def find_resource | |
scoped_collection.friendly.find(params[:id]) | |
end | |
end | |
form do |f| | |
f.inputs "Main info" do | |
f.input :category | |
f.input :name | |
f.input :description | |
f.input :text | |
f.input :in_slider, :label => "Product in slider on Main Page" | |
f.input :tags, # Show all tags AND checked already selected one (by relations through :tags - input must named :tags) | |
as: :select, | |
multiple: :true, | |
collection: ActsAsTaggableOn::Tag.select(:id, :name).all | |
end | |
f.actions | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.... | |
module MyApp | |
class Application < Rails::Application | |
config.active_record.observers = :product_observer, :tagging_observer | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Product < ActiveRecord::Base | |
# Using friendly_id | |
extend FriendlyId | |
friendly_id :name, use: :slugged | |
# Product belongs to Category. So tags have a :tagger_id and :tagger_type too, and should be saved through object of Category. | |
belongs_to :category | |
# Using acts_as_taggable_on gem | |
acts_as_taggable # Alias for acts_as_taggable_on :tags | |
acts_as_taggable_on :tags, :skills | |
# Renamed a new slug after change :name field | |
def should_generate_new_friendly_id? | |
slug.blank? || name_changed? | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TaggingObserver < ActiveRecord::Observer | |
observe ActsAsTaggableOn::Tagging | |
# Before save add :tagger_id using refs on the object witch 'taggable' and owner of this 'taggable' | |
def before_save(tagging) | |
tagging.tagger = tagging.taggable.category if (tagging.taggable.respond_to?(:category) and tagging.tagger != tagging.taggable.category) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is to edit the tags on the admin.
app/admin/tags.rb