easy_mongoid_tag 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (70) hide show
  1. data/.gitignore +18 -0
  2. data/Gemfile +3 -0
  3. data/Gemfile.lock +89 -0
  4. data/Guardfile +22 -0
  5. data/LICENSE +191 -0
  6. data/README.textile +34 -0
  7. data/easy_mongoid_tag.gemspec +29 -0
  8. data/lib/easy_mongoid_tag.rb +5 -0
  9. data/lib/easy_mongoid_tag/tag.rb +151 -0
  10. data/lib/easy_mongoid_tag/version.rb +4 -0
  11. data/lib/mongoid_extensions/extension_field.rb +14 -0
  12. data/prototype/book.rb +119 -0
  13. data/prototype/mongodb.yml +6 -0
  14. data/spec/function_spec.rb +43 -0
  15. data/spec/mongodb.yml +6 -0
  16. data/spec/spec_helper.rb +54 -0
  17. data/spec/tag_spec.rb +98 -0
  18. data/test/read_book/.gitignore +15 -0
  19. data/test/read_book/Gemfile +39 -0
  20. data/test/read_book/Gemfile.lock +119 -0
  21. data/test/read_book/README.rdoc +261 -0
  22. data/test/read_book/Rakefile +7 -0
  23. data/test/read_book/app/assets/images/rails.png +0 -0
  24. data/test/read_book/app/assets/javascripts/application.js +15 -0
  25. data/test/read_book/app/assets/stylesheets/application.css +13 -0
  26. data/test/read_book/app/controllers/application_controller.rb +3 -0
  27. data/test/read_book/app/helpers/application_helper.rb +2 -0
  28. data/test/read_book/app/mailers/.gitkeep +0 -0
  29. data/test/read_book/app/models/.gitkeep +0 -0
  30. data/test/read_book/app/models/book.rb +11 -0
  31. data/test/read_book/app/views/layouts/application.html.erb +14 -0
  32. data/test/read_book/config.ru +4 -0
  33. data/test/read_book/config/application.rb +68 -0
  34. data/test/read_book/config/boot.rb +6 -0
  35. data/test/read_book/config/environment.rb +5 -0
  36. data/test/read_book/config/environments/development.rb +31 -0
  37. data/test/read_book/config/environments/production.rb +64 -0
  38. data/test/read_book/config/environments/test.rb +35 -0
  39. data/test/read_book/config/initializers/backtrace_silencers.rb +7 -0
  40. data/test/read_book/config/initializers/inflections.rb +15 -0
  41. data/test/read_book/config/initializers/mime_types.rb +5 -0
  42. data/test/read_book/config/initializers/secret_token.rb +7 -0
  43. data/test/read_book/config/initializers/session_store.rb +8 -0
  44. data/test/read_book/config/initializers/wrap_parameters.rb +10 -0
  45. data/test/read_book/config/locales/en.yml +5 -0
  46. data/test/read_book/config/mongoid.yml +80 -0
  47. data/test/read_book/config/routes.rb +58 -0
  48. data/test/read_book/db/seeds.rb +7 -0
  49. data/test/read_book/lib/assets/.gitkeep +0 -0
  50. data/test/read_book/lib/tasks/.gitkeep +0 -0
  51. data/test/read_book/log/.gitkeep +0 -0
  52. data/test/read_book/public/404.html +26 -0
  53. data/test/read_book/public/422.html +26 -0
  54. data/test/read_book/public/500.html +25 -0
  55. data/test/read_book/public/favicon.ico +0 -0
  56. data/test/read_book/public/index.html +241 -0
  57. data/test/read_book/public/robots.txt +5 -0
  58. data/test/read_book/script/rails +6 -0
  59. data/test/read_book/test/fixtures/.gitkeep +0 -0
  60. data/test/read_book/test/fixtures/books.yml +11 -0
  61. data/test/read_book/test/functional/.gitkeep +0 -0
  62. data/test/read_book/test/integration/.gitkeep +0 -0
  63. data/test/read_book/test/performance/browsing_test.rb +12 -0
  64. data/test/read_book/test/test_helper.rb +7 -0
  65. data/test/read_book/test/unit/.gitkeep +0 -0
  66. data/test/read_book/test/unit/book_test.rb +7 -0
  67. data/test/read_book/vendor/assets/javascripts/.gitkeep +0 -0
  68. data/test/read_book/vendor/assets/stylesheets/.gitkeep +0 -0
  69. data/test/read_book/vendor/plugins/.gitkeep +0 -0
  70. metadata +282 -0
@@ -0,0 +1,4 @@
1
+ module EasyMongoidTag
2
+ VERSION = '0.0.1'
3
+ end
4
+
@@ -0,0 +1,14 @@
1
+ module EasyMongoidTag
2
+ module Mongoid
3
+ module Fields
4
+ def field name, options
5
+ if options[:type] == EasyMongoidTag::Tag
6
+ p "ok i'am in"
7
+ return
8
+ else
9
+ super
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
data/prototype/book.rb ADDED
@@ -0,0 +1,119 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require 'rubygems'
4
+ require 'mongoid'
5
+
6
+ # prototype
7
+ Mongoid.load!("mongodb.yml", :development)
8
+
9
+ extend ActiveSupport::Concern
10
+
11
+ class Book
12
+
13
+ include Mongoid::Document
14
+
15
+ field :bname, as: :book_name, type: String
16
+
17
+ field :author_ids, type: Array
18
+
19
+ field :category_ids, type: Array
20
+
21
+ class_attribute :tag_items
22
+
23
+ self.tag_items ||= []
24
+ self.tag_items << :author_ids
25
+
26
+ self.tag_items ||= []
27
+ self.tag_items << :category_ids
28
+
29
+ index({ author_ids: 1 })
30
+ index({ category_ids: 1 })
31
+
32
+ before_validation do |doc|
33
+ doc.category_ids = doc.category_ids.map do |name|
34
+ name.strip!
35
+ tag = CategryTag.where(name: name).first
36
+ if tag
37
+ tag.id
38
+ else
39
+ tag = CategryTag.new(name: name)
40
+ tag.save ? tag.id : nil
41
+ end
42
+ end.select(&:present?)
43
+ end
44
+
45
+ def authors
46
+ AuthorTag.find(*self.author_ids)
47
+ end
48
+
49
+ def categories
50
+ CategryTag.find(*self.category_ids)
51
+ end
52
+ end
53
+
54
+
55
+ class AuthorTag
56
+ include Mongoid::Document
57
+
58
+ field :name, type: String
59
+
60
+ index({ name: 1 }, { unique: true, name: "name_index" })
61
+
62
+ before_validation do |record|
63
+ record.name = record.name.gsub(/\s/, '')
64
+ end
65
+
66
+
67
+ def books
68
+ Book.where(:authors => self.id)
69
+ end
70
+
71
+ class << self
72
+ def search key_word
73
+ self.any_of(name: /.*#{key_word}.*/)
74
+ end
75
+ end
76
+ end
77
+
78
+ class CategryTag
79
+ include Mongoid::Document
80
+
81
+ include Mongoid::Timestamps::Created
82
+
83
+ field :name, type: String
84
+
85
+ index({ name: 1 }, { unique: true, name: "name_index" })
86
+
87
+ before_validation do |record|
88
+ record.name = record.name.strip
89
+ end
90
+
91
+ validates :name, presence: true
92
+
93
+ def books
94
+ Book.where(:authors => self.id)
95
+ end
96
+
97
+ class << self
98
+ def search key_word
99
+ self.any_of(name: /.*#{key_word}.*/)
100
+ end
101
+ end
102
+ end
103
+
104
+ b1 = Book.find_or_create_by( :bname => '红楼梦',
105
+ :category_ids => ['四大名著', ' 古典文学', '人人都爱wadexing '] )
106
+
107
+
108
+ p b1.authors
109
+ p b1.categories
110
+
111
+ Book.create_indexes
112
+ AuthorTag.create_indexes
113
+ CategryTag.create_indexes
114
+
115
+ p Book.fields
116
+ p CategryTag.search('四').first
117
+ p AuthorTag.last.books.map(&:bname)
118
+ #p AuthorTag.search('西').first
119
+ #p CategryTag.search('小说').first
@@ -0,0 +1,6 @@
1
+ development:
2
+ sessions:
3
+ default:
4
+ database: easy_mongoid_tag
5
+ hosts:
6
+ - localhost:27017
@@ -0,0 +1,43 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'spec_helper'
3
+
4
+ describe "功能" do
5
+
6
+ before(:all) do
7
+ Mongoid.load!("/Users/wade/RubyLab/easy_mongoid_tag/spec/mongodb.yml", :test)
8
+
9
+ class Model
10
+ include Mongoid::Document
11
+ include EasyMongoidTag
12
+
13
+ field :bname, as: :book_name, type: String
14
+ easy_tags :tests
15
+ end
16
+
17
+ @book1 = Model.create(bname: '红楼梦', :tests => ['曹雪芹 ', ' 高 鹗 '])
18
+ @book2 = Model.create(bname: '石头记', :tests => ['曹沾', '高 鹗'])
19
+ end
20
+
21
+ it "应 可以找到 所有的标签" do
22
+ @book1.test_tags.map{|tag| tag.title }.should include('曹雪芹', '高 鹗')
23
+ @book2.test_tags.map{|tag| tag.title }.should include('曹沾', '高 鹗')
24
+ end
25
+
26
+ it "应 可以通过标签得到 model" do
27
+ TestTag.find_by(title: '曹雪芹').models.map(&:book_name).should include('红楼梦')
28
+ TestTag.find_by(title: '曹沾').models.map(&:book_name).should include('石头记')
29
+ end
30
+
31
+
32
+ it "应 标签搜索" do
33
+ TestTag.search('曹').map(&:title).should include('曹雪芹', '曹沾')
34
+ end
35
+
36
+
37
+ it "当标签被删除 应 删除模型字段中的相应引用" do
38
+ ge = TestTag.find_by(title: '高 鹗')
39
+ ge.destroy
40
+ @book1.reload.tests.should_not include(ge.id)
41
+ end
42
+
43
+ end
data/spec/mongodb.yml ADDED
@@ -0,0 +1,6 @@
1
+ test:
2
+ sessions:
3
+ default:
4
+ database: easy_mongoid_tag
5
+ hosts:
6
+ - localhost:27017
@@ -0,0 +1,54 @@
1
+ require 'rubygems'
2
+ require 'spork'
3
+ #uncomment the following line to use spork with the debugger
4
+ #require 'spork/ext/ruby-debug'
5
+
6
+ Spork.prefork do
7
+ # Loading more in this block will cause your tests to run faster. However,
8
+ # if you change any configuration or code from libraries loaded here, you'll
9
+ # need to restart spork for it take effect.
10
+
11
+ end
12
+
13
+ Spork.each_run do
14
+ # This code will be run each time you run your specs.
15
+
16
+ end
17
+
18
+ # --- Instructions ---
19
+ # Sort the contents of this file into a Spork.prefork and a Spork.each_run
20
+ # block.
21
+ #
22
+ # The Spork.prefork block is run only once when the spork server is started.
23
+ # You typically want to place most of your (slow) initializer code in here, in
24
+ # particular, require'ing any 3rd-party gems that you don't normally modify
25
+ # during development.
26
+ #
27
+ # The Spork.each_run block is run each time you run your specs. In case you
28
+ # need to load files that tend to change during development, require them here.
29
+ # With Rails, your application modules are loaded automatically, so sometimes
30
+ # this block can remain empty.
31
+ #
32
+ # Note: You can modify files loaded *from* the Spork.each_run block without
33
+ # restarting the spork server. However, this file itself will not be reloaded,
34
+ # so if you change any of the code inside the each_run block, you still need to
35
+ # restart the server. In general, if you have non-trivial code in this file,
36
+ # it's advisable to move it into a separate file so you can easily edit it
37
+ # without restarting spork. (For example, with RSpec, you could move
38
+ # non-trivial code into a file spec/support/my_helper.rb, making sure that the
39
+ # spec/support/* files are require'd from inside the each_run block.)
40
+ #
41
+ # Any code that is left outside the two blocks will be run during preforking
42
+ # *and* during each_run -- that's probably not what you want.
43
+ #
44
+ # These instructions should self-destruct in 10 seconds. If they don't, feel
45
+ # free to delete them.
46
+
47
+ $:.unshift File.expand_path('../../lib', __FILE__)
48
+
49
+ require 'easy_mongoid_tag'
50
+ require 'mongoid-rspec'
51
+
52
+ RSpec.configure do |configuration|
53
+ configuration.include Mongoid::Matchers
54
+ end
data/spec/tag_spec.rb ADDED
@@ -0,0 +1,98 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'spec_helper'
3
+
4
+ describe EasyMongoidTag do
5
+ describe '混入此模块' do
6
+ context '当 混入非Mongoid::Document类' do
7
+ it '应 抛出异常' do
8
+ expect {
9
+ class P
10
+ include EasyMongoidTag
11
+ end
12
+ }.to raise_exception(RuntimeError,
13
+ "EasyMongoidTag is mixined A non-mongoid_document")
14
+
15
+ end
16
+ end
17
+
18
+
19
+ context "当 混入Mongoid::Docutment类" do
20
+ before(:all) do
21
+ class Book
22
+ include Mongoid::Document
23
+ include EasyMongoidTag
24
+
25
+ easy_tag :authors
26
+ end
27
+ end
28
+
29
+ describe "混入类" do
30
+ it "应 含有类宏" do
31
+ Book.should respond_to(:easy_tag)
32
+ end
33
+
34
+ it "应 有Array型标签字段" do
35
+ Book.should have_field(:authors).of_type(Array)
36
+ end
37
+
38
+ it "应 在class属性tag_items中包含标签名称" do
39
+ Book.tag_items.should include(:authors)
40
+ end
41
+
42
+ it "应 在标签字段上设置索引" do
43
+ Book.should have_index_for(authors: 1)
44
+ end
45
+
46
+ it "应 有与tag同名的实例方法" do
47
+ Book.new.should respond_to(:author_tags)
48
+ end
49
+
50
+ end
51
+
52
+ describe "生成标签类" do
53
+ context "When 标签类已经存在" do
54
+ it "抛出异常" do
55
+ expect do
56
+ class ActorTag; end
57
+
58
+ class Movie
59
+ include Mongoid::Document
60
+ include EasyMongoidTag
61
+ easy_tag :actors
62
+ end
63
+ end.to raise_error(RuntimeError, "Class ActorTag already existed!")
64
+
65
+ end
66
+ end
67
+
68
+ context "When 标签类不存在" do
69
+ it "应 生成标签类" do
70
+ AuthorTag
71
+ end
72
+
73
+ describe "标签类" do
74
+ it "应 是一个Mongoid::Document" do
75
+ AuthorTag.included_modules.should include(Mongoid::Document)
76
+ end
77
+
78
+ it "应 有 一个String型的title field " do
79
+ AuthorTag.should have_field(:title).of_type(String)
80
+ end
81
+
82
+ it "应 在title字段上设置 唯一性索引" do
83
+ AuthorTag.should have_index_for(title: 1).with_options(unique: true)
84
+ end
85
+
86
+ it "应 包含一个 反查模型的实例方法" do
87
+ AuthorTag.new.should respond_to(:books)
88
+ end
89
+
90
+ it "应 包含一个 可用于模糊搜索的类方法" do
91
+ AuthorTag.should respond_to(:search)
92
+ end
93
+ end
94
+ end
95
+ end
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,15 @@
1
+ # See http://help.github.com/ignore-files/ for more about ignoring files.
2
+ #
3
+ # If you find yourself ignoring temporary files generated by your text editor
4
+ # or operating system, you probably want to add a global ignore instead:
5
+ # git config --global core.excludesfile ~/.gitignore_global
6
+
7
+ # Ignore bundler config
8
+ /.bundle
9
+
10
+ # Ignore the default SQLite database.
11
+ /db/*.sqlite3
12
+
13
+ # Ignore all logfiles and tempfiles.
14
+ /log/*.log
15
+ /tmp
@@ -0,0 +1,39 @@
1
+ source 'http://ruby.taobao.org/'
2
+
3
+ gem 'rails', '3.2.14'
4
+
5
+ gem 'mongoid'
6
+ gem 'easy_mongoid_tag'
7
+
8
+ # Bundle edge Rails instead:
9
+ # gem 'rails', :git => 'git://github.com/rails/rails.git'
10
+
11
+
12
+ # Gems used only for assets and not required
13
+ # in production environments by default.
14
+ group :assets do
15
+ gem 'sass-rails', '~> 3.2.3'
16
+ gem 'coffee-rails', '~> 3.2.1'
17
+
18
+ # See https://github.com/sstephenson/execjs#readme for more supported runtimes
19
+ # gem 'therubyracer', :platforms => :ruby
20
+
21
+ gem 'uglifier', '>= 1.0.3'
22
+ end
23
+
24
+ gem 'jquery-rails'
25
+
26
+ # To use ActiveModel has_secure_password
27
+ # gem 'bcrypt-ruby', '~> 3.0.0'
28
+
29
+ # To use Jbuilder templates for JSON
30
+ # gem 'jbuilder'
31
+
32
+ # Use unicorn as the app server
33
+ # gem 'unicorn'
34
+
35
+ # Deploy with Capistrano
36
+ # gem 'capistrano'
37
+
38
+ # To use debugger
39
+ # gem 'debugger'
@@ -0,0 +1,119 @@
1
+ GEM
2
+ remote: http://ruby.taobao.org/
3
+ specs:
4
+ actionmailer (3.2.14)
5
+ actionpack (= 3.2.14)
6
+ mail (~> 2.5.4)
7
+ actionpack (3.2.14)
8
+ activemodel (= 3.2.14)
9
+ activesupport (= 3.2.14)
10
+ builder (~> 3.0.0)
11
+ erubis (~> 2.7.0)
12
+ journey (~> 1.0.4)
13
+ rack (~> 1.4.5)
14
+ rack-cache (~> 1.2)
15
+ rack-test (~> 0.6.1)
16
+ sprockets (~> 2.2.1)
17
+ activemodel (3.2.14)
18
+ activesupport (= 3.2.14)
19
+ builder (~> 3.0.0)
20
+ activerecord (3.2.14)
21
+ activemodel (= 3.2.14)
22
+ activesupport (= 3.2.14)
23
+ arel (~> 3.0.2)
24
+ tzinfo (~> 0.3.29)
25
+ activeresource (3.2.14)
26
+ activemodel (= 3.2.14)
27
+ activesupport (= 3.2.14)
28
+ activesupport (3.2.14)
29
+ i18n (~> 0.6, >= 0.6.4)
30
+ multi_json (~> 1.0)
31
+ arel (3.0.2)
32
+ builder (3.0.4)
33
+ coffee-rails (3.2.2)
34
+ coffee-script (>= 2.2.0)
35
+ railties (~> 3.2.0)
36
+ coffee-script (2.2.0)
37
+ coffee-script-source
38
+ execjs
39
+ coffee-script-source (1.6.3)
40
+ easy_mongoid_tag (0.0.1)
41
+ mongoid (~> 3.1.0)
42
+ erubis (2.7.0)
43
+ execjs (2.0.1)
44
+ hike (1.2.3)
45
+ i18n (0.6.5)
46
+ journey (1.0.4)
47
+ jquery-rails (2.0.2)
48
+ railties (>= 3.2.0, < 5.0)
49
+ thor (~> 0.14)
50
+ json (1.8.0)
51
+ mail (2.5.4)
52
+ mime-types (~> 1.16)
53
+ treetop (~> 1.4.8)
54
+ mime-types (1.25)
55
+ mongoid (3.1.4)
56
+ activemodel (~> 3.2)
57
+ moped (~> 1.4)
58
+ origin (~> 1.0)
59
+ tzinfo (~> 0.3.22)
60
+ moped (1.5.1)
61
+ multi_json (1.7.9)
62
+ origin (1.1.0)
63
+ polyglot (0.3.3)
64
+ rack (1.4.5)
65
+ rack-cache (1.2)
66
+ rack (>= 0.4)
67
+ rack-ssl (1.3.3)
68
+ rack
69
+ rack-test (0.6.2)
70
+ rack (>= 1.0)
71
+ rails (3.2.14)
72
+ actionmailer (= 3.2.14)
73
+ actionpack (= 3.2.14)
74
+ activerecord (= 3.2.14)
75
+ activeresource (= 3.2.14)
76
+ activesupport (= 3.2.14)
77
+ bundler (~> 1.0)
78
+ railties (= 3.2.14)
79
+ railties (3.2.14)
80
+ actionpack (= 3.2.14)
81
+ activesupport (= 3.2.14)
82
+ rack-ssl (~> 1.3.2)
83
+ rake (>= 0.8.7)
84
+ rdoc (~> 3.4)
85
+ thor (>= 0.14.6, < 2.0)
86
+ rake (10.1.0)
87
+ rdoc (3.12.2)
88
+ json (~> 1.4)
89
+ sass (3.2.10)
90
+ sass-rails (3.2.6)
91
+ railties (~> 3.2.0)
92
+ sass (>= 3.1.10)
93
+ tilt (~> 1.3)
94
+ sprockets (2.2.2)
95
+ hike (~> 1.2)
96
+ multi_json (~> 1.0)
97
+ rack (~> 1.0)
98
+ tilt (~> 1.1, != 1.3.0)
99
+ thor (0.18.1)
100
+ tilt (1.4.1)
101
+ treetop (1.4.15)
102
+ polyglot
103
+ polyglot (>= 0.3.1)
104
+ tzinfo (0.3.37)
105
+ uglifier (2.2.0)
106
+ execjs (>= 0.3.0)
107
+ multi_json (~> 1.0, >= 1.0.2)
108
+
109
+ PLATFORMS
110
+ ruby
111
+
112
+ DEPENDENCIES
113
+ coffee-rails (~> 3.2.1)
114
+ easy_mongoid_tag
115
+ jquery-rails
116
+ mongoid
117
+ rails (= 3.2.14)
118
+ sass-rails (~> 3.2.3)
119
+ uglifier (>= 1.0.3)