ruby-on-railsrubyacts-as-taggable

How can I scope acts_as_taggable_on_stereoids by Account?


If I have my website logically split into accounts (eg. acme.mywebsite.com, xyz.mywebsite.com), how can I implement act-as-taggable-on-steroids and have the tags scoped by the current account ?

To give little more details, if I am accessing acme I don't want to see tags from xyz subdomain.

I have looked into act-as-taggable-on, but the context only is provided if you want to have different classes of tags for the same model.


Solution

  • Assuming I understood your question in that you have multiple accounts each with their own set of tags that could be applied to any model in your application that calls acts_as_taggable. The following should do what You want.

    You can add the following to application controller to make the subdomain accessible to all actions.

    class ApplicationController < ActionController::Base
    
      before_filter :getSubdomain
    
      def getSubdomain
          @current_subdomain.(self.request.subdomains[0])
      end
    end
    

    Assuming you link a tag to a subdomain some how in your database, you can then create a named scope. This example assumes that the subdomain is a user name and your tagging model belongs to a user, you can then use a named scope on your tag model to select only those relevant to the subdomain.

    class Tag < ActiveRecord::Base
      ...
      named_scope :find_by_subdomain, labmda do |subdomain|
        { :joins => "users ON users.id = tags.user_id", :conditions => ["users.name = ?", subdomain] }
      end
    end
    

    Then to retrieve tags on Posts that were created by the user associated with the subdomain:Posts.tags.find_by_subdomain(@subdomain)

    NB: you'll have to augment the tag model supplied by acts-as-taggable-on-steroids to add the following. - A column linking each tag to an account. - The uniqueness validation to scope on account. Allowing multiple accounts to have the same tags.