Giter VIP home page Giter VIP logo

mongoid-tags-arent-hard's Introduction

Mongoid::TagsArentHard

A tagging gem for Mongoid 3 that doesn't actually suck.

For some reason all of the tagging gems for Mongoid suck. Not sure why, it's really not that hard a problem to solve. One of the biggest complaints I have is that you can't have a model that has two types of "tags". The other problem I have with the other gems is that I want to be able to set my tags equal to a string and have it become an Array, and vice versa. This gem solves both of those problems.

Installation

Add this line to your application's Gemfile:

gem 'mongoid-tags-arent-hard'

And then execute:

$ bundle

Or install it yourself as:

$ gem install mongoid-tags-arent-hard

Usage

To add tags to a model you need to first include the Mongoid::TagsArentHard module and then define what you want the field to be called using the taggable_with method.

class Foo
  include Mongoid::Document
  include Mongoid::TagsArentHard

  taggable_with :tags
  taggable_with :colors, separator: ";"
end

Now we have two different types of "tags"; the first being called tags and the second being called colors. We have also told the colors to use ";" as its separator.

Now we can do fun things like this:

# set with either a string or an array:
foo = Foo.new(tags: "a,b,c", colors: ["red", "blue"])

# retrieve the list:
foo.tags #=> ["a", "b", "c"]
foo.colors #=> ["red", "blue"]

# append with either a string or an array:
foo.tags << "d,e"
foo.tags #=> ["a", "b", "c", "d", "e"]
foo.colors << ["green", "yellow"]
foo.colors #=> ["red", "blue", "green", "yellow"]

# set with either a string or an array:
foo.tags = ["x", "y", "z"]
foo.tags #=> ["x", "y", "z"]
foo.colors = "black;brown"
foo.colors #=> ["black", "brown"]

Searching

There are a few scopes included that make it easy to find objects that have the tags you are looking for. These methods are generated using the name of the field you designed, so in our previous example we would have the following methods available to us:

# Find objects with any of the values:
Foo.with_any_tags("a")
Foo.with_any_tags(["a", "b"])
Foo.with_any_tags("a, b")
Foo.with_any_colors("a")
Foo.with_any_colors(["a", "b"])
Foo.with_any_colors("a, b")

# Find objects with all of these values:
Foo.with_all_tags("a")
Foo.with_all_tags(["a", "b"])
Foo.with_all_tags("a, b")
Foo.with_all_colors("a")
Foo.with_all_colors(["a", "b"])
Foo.with_all_colors("a, b")

# Find objects without any of the values:
Foo.without_any_tags("a")
Foo.without_any_tags(["a", "b"])
Foo.without_any_tags("a, b")
Foo.without_any_colors("a")
Foo.without_any_colors(["a", "b"])
Foo.without_any_colors("a, b")

# Retrieve a distinct array of all tags
Foo.all_tags
Foo.where(name: 'test').all_tags

Again, notice that you can use either a string, an array, or a splatted list as values to these scopes.

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Write your tests
  4. Commit your changes (git commit -am 'Add some feature')
  5. Push to the branch (git push origin my-new-feature)
  6. Create new Pull Request

Contributers

  • Mark Bates
  • Luke Bergen
  • Dave South
  • Carsten Block
  • Laurent Arnoud
  • Kevin Incorvia
  • thomas morgan

mongoid-tags-arent-hard's People

Contributors

cblock avatar lukebergen avatar markbates avatar spk avatar zarqman avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

mongoid-tags-arent-hard's Issues

Indexing; use of map-reduce?

With an at a glance comparison, I'm looking at (1) mongoid_taggable, (2) mongoid-simple-tags, and (3) this.

this seems like the most straightforward and flexible, but mentions nothing about indexing or map-reduce in the readme.

from my limited understanding, these are techniques used to improve the speed of queries.

Is something like this already implemented, going to be built later, or not going to be implemented?

I'm sure other people reading the readme might also have these questions, so it would be good to include this answer there too.


I'll probably be using this, but it'd be nice to know as I'm diving in.

Add #rename method

This method should simply rename a tag:
Foo.rename_tags('old tag name', 'new tag name')

And it should work with scoped queries too:

Foo.where(name: 'test').rename_tags('old tag name', 'new tag name')
Foo.where(name: 'test').rename_colors('old tag name', 'new tag name')

Add #delete method

This method simply removes a tag from all selected records:

Foo.delete_tags('tag name')
Foo.delete_colors('tag name')

Foo.where(name: 'some name').delete_tags('tag name')

Scope for unique tag list

Hi and thanks for this really nice plugin.

Just wondering if there's another scope planned that eases the retrieval of a unique tag list along the lines of the already existing search scopes?

For example

foo1.tags = ["x", "y"]
foo1.name = "baz"
foo2.tags = ["y,z"]
foo2.name = ""

Foo.tags #=> should be ["x","y","z"]
Foo.where(name: "baz").tags #=> should be ["x","y"]

Doesn't work with Rails_admin

Full trace:

Type Mongoid::TagsArentHard::Tags for field :tags in MyModel not supported

rails_admin (0.5.0) lib/rails_admin/adapters/mongoid.rb:310:in `type_lookup'
rails_admin (0.5.0) lib/rails_admin/adapters/mongoid.rb:92:in `block in properties'
rails_admin (0.5.0) lib/rails_admin/adapters/mongoid.rb:85:in `each'
rails_admin (0.5.0) lib/rails_admin/adapters/mongoid.rb:85:in `map'
rails_admin (0.5.0) lib/rails_admin/adapters/mongoid.rb:85:in `properties'
rails_admin (0.5.0) lib/rails_admin/config/actions/dashboard.rb:29:in `block (3 levels) in <class:Dashboard>'
rails_admin (0.5.0) lib/rails_admin/config/actions/dashboard.rb:24:in `each'
rails_admin (0.5.0) lib/rails_admin/config/actions/dashboard.rb:24:in `block (2 levels) in <class:Dashboard>'
(eval):8:in `instance_eval'
(eval):8:in `dashboard'
actionpack (4.0.0) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (4.0.0) lib/abstract_controller/base.rb:189:in `process_action'
actionpack (4.0.0) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.0.0) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (4.0.0) lib/active_support/callbacks.rb:443:in `_run__2739951418677313808__process_action__callbacks'
activesupport (4.0.0) lib/active_support/callbacks.rb:80:in `run_callbacks'
actionpack (4.0.0) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (4.0.0) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.0.0) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
activesupport (4.0.0) lib/active_support/notifications.rb:159:in `block in instrument'
activesupport (4.0.0) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.0.0) lib/active_support/notifications.rb:159:in `instrument'
actionpack (4.0.0) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.0.0) lib/action_controller/metal/params_wrapper.rb:245:in `process_action'
actionpack (4.0.0) lib/abstract_controller/base.rb:136:in `process'
actionpack (4.0.0) lib/abstract_controller/rendering.rb:44:in `process'
actionpack (4.0.0) lib/action_controller/metal.rb:195:in `dispatch'
actionpack (4.0.0) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.0.0) lib/action_controller/metal.rb:231:in `block in action'
actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:80:in `call'
actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:80:in `dispatch'
actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:48:in `call'
actionpack (4.0.0) lib/action_dispatch/journey/router.rb:71:in `block in call'
actionpack (4.0.0) lib/action_dispatch/journey/router.rb:59:in `each'
actionpack (4.0.0) lib/action_dispatch/journey/router.rb:59:in `call'
actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:655:in `call'
railties (4.0.0) lib/rails/engine.rb:511:in `call'
railties (4.0.0) lib/rails/railtie/configurable.rb:30:in `method_missing'
actionpack (4.0.0) lib/action_dispatch/journey/router.rb:71:in `block in call'
actionpack (4.0.0) lib/action_dispatch/journey/router.rb:59:in `each'
actionpack (4.0.0) lib/action_dispatch/journey/router.rb:59:in `call'
actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:655:in `call'
rack-pjax (0.7.0) lib/rack/pjax.rb:12:in `call'
/Users/dparker/.rvm/gems/ruby-2.0.0-p247/bundler/gems/mongoid-d52c33bce36a/lib/rack/mongoid/middleware/identity_map.rb:34:in `block in call'
/Users/dparker/.rvm/gems/ruby-2.0.0-p247/bundler/gems/mongoid-d52c33bce36a/lib/mongoid/unit_of_work.rb:39:in `unit_of_work'
/Users/dparker/.rvm/gems/ruby-2.0.0-p247/bundler/gems/mongoid-d52c33bce36a/lib/rack/mongoid/middleware/identity_map.rb:34:in `call'
warden (1.2.1) lib/warden/manager.rb:35:in `block in call'
warden (1.2.1) lib/warden/manager.rb:34:in `catch'
warden (1.2.1) lib/warden/manager.rb:34:in `call'
rack (1.5.2) lib/rack/etag.rb:23:in `call'
rack (1.5.2) lib/rack/conditionalget.rb:25:in `call'
rack (1.5.2) lib/rack/head.rb:11:in `call'
remotipart (1.1.1) lib/remotipart/middleware.rb:25:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/flash.rb:241:in `call'
rack (1.5.2) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/cookies.rb:486:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.0.0) lib/active_support/callbacks.rb:373:in `_run__226034610281748181__call__callbacks'
activesupport (4.0.0) lib/active_support/callbacks.rb:80:in `run_callbacks'
actionpack (4.0.0) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/reloader.rb:64:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.0.0) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.0.0) lib/rails/rack/logger.rb:21:in `block in call'
activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `block in tagged'
activesupport (4.0.0) lib/active_support/tagged_logging.rb:25:in `tagged'
activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `tagged'
railties (4.0.0) lib/rails/rack/logger.rb:21:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
rack (1.5.2) lib/rack/runtime.rb:17:in `call'
activesupport (4.0.0) lib/active_support/cache/strategy/local_cache.rb:83:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/static.rb:64:in `call'
railties (4.0.0) lib/rails/engine.rb:511:in `call'
railties (4.0.0) lib/rails/application.rb:97:in `call'
railties (4.0.0) lib/rails/railtie/configurable.rb:30:in `method_missing'
rack (1.5.2) lib/rack/lint.rb:49:in `_call'
rack (1.5.2) lib/rack/lint.rb:37:in `call'
rack (1.5.2) lib/rack/showexceptions.rb:24:in `call'
rack (1.5.2) lib/rack/commonlogger.rb:33:in `call'
rack (1.5.2) lib/rack/chunked.rb:43:in `call'
rack (1.5.2) lib/rack/content_length.rb:14:in `call'
unicorn (4.6.3) lib/unicorn/http_server.rb:552:in `process_client'
unicorn (4.6.3) lib/unicorn/http_server.rb:632:in `worker_loop'
unicorn (4.6.3) lib/unicorn/http_server.rb:500:in `spawn_missing_workers'
unicorn (4.6.3) lib/unicorn/http_server.rb:142:in `start'
unicorn (4.6.3) bin/unicorn:126:in `<top (required)>'
/Users/dparker/.rvm/gems/ruby-2.0.0-p247/bin/unicorn:23:in `load'
/Users/dparker/.rvm/gems/ruby-2.0.0-p247/bin/unicorn:23:in `<main>'
/Users/dparker/.rvm/gems/ruby-2.0.0-p247/bin/ruby_noexec_wrapper:14:in `eval'
/Users/dparker/.rvm/gems/ruby-2.0.0-p247/bin/ruby_noexec_wrapper:14:in `<main>'

Important Gems:

ruby '2.0.0'

gem 'rails', '4.0.0'
gem 'mongoid', git: 'https://github.com/mongoid/mongoid.git'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.