Guest User

Untitled

a guest
Jan 17th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. config.autoload_paths += %W(#{Rails.root}/app/workers
  2. #{Rails.root}/lib/validators
  3. #{Rails.root}/lib/content_items
  4. #{Rails.root}/lib/mongoid
  5. #{Rails.root}/lib/booher_modules
  6. )
  7.  
  8. ## tree
  9. .
  10. ├── booher_modules
  11. │   └── voting_helpers.rb
  12. ├── campus_authorization.rb
  13. ├── configuration.rb
  14. ├── content_items
  15. │   ├── content_item.rb
  16. │   └── cover_picture.rb
  17. ├── development_mail_interceptor.rb
  18. ├── mongoid
  19. │   └── counter_cache.rb
  20. ├── paperclip_processors
  21. │   └── cropper.rb
  22. ├── special_characters.rb
  23. ├── string_extensions.rb
  24. ├── tasks
  25. │   ├── assets.thor
  26. │   ├── cba.rake
  27. │   ├── cucumber.rake
  28. │   ├── data.rake
  29. │   ├── delayed_jobs.rake
  30. │   ├── deploy.thor
  31. │   ├── set_data.rake
  32. │   └── test.thor
  33. ├── translator
  34. │   └── translator.rb
  35. └── validators
  36. └── email_validator.rb
  37.  
  38. ## model
  39.  
  40. class Vote
  41. include Mongoid::Document
  42. include Mongoid::Timestamps
  43. include Mongoid::CounterCache
  44.  
  45. field :value, :type => Symbol # can be :aye, :nay, :abstain, :present
  46.  
  47. counter_cache :name => 'polco_group', :field => 'vote_count'
  48.  
  49. ## module
  50.  
  51.  
  52. module Mongoid
  53. module CounterCache
  54. extend ActiveSupport::Concern
  55.  
  56. module ClassMethods
  57. def counter_cache(options)
  58. name = options[:name]
  59. counter_field = options[:field]
  60.  
  61. after_create do |document|
  62. relation = document.send(name)
  63. relation.collection.update(relation._selector, {'$inc' => {counter_field.to_s => 1}})
  64. end
Add Comment
Please, Sign In to add comment