mn_model 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +42 -0
- data/CHANGELOG.rdoc +2 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +69 -0
- data/Guardfile +24 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +41 -0
- data/config/database.yml +7 -0
- data/db/migrate/0001_create_notes.rb +29 -0
- data/lib/mn_model.rb +23 -0
- data/lib/mn_model/entry.rb +24 -0
- data/lib/mn_model/field.rb +10 -0
- data/lib/mn_model/item.rb +13 -0
- data/lib/mn_model/note.rb +72 -0
- data/lib/mn_model/version.rb +3 -0
- data/mn_model.gemspec +31 -0
- data/test/mn_model/test_entry.rb +125 -0
- data/test/mn_model/test_field.rb +52 -0
- data/test/mn_model/test_item.rb +51 -0
- data/test/mn_model/test_note.rb +43 -0
- metadata +153 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: 95805ff64ff51f737a7e5260cf49d9ca418cb967
|
|
4
|
+
data.tar.gz: 90d6f8018680235762627474e7e58d0669aa9a0c
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 061473286df709b65c577009e92156e8ecc906117a438c639c858b986e2e025b2d80b34ff21365675c46856a7253cf1a5465bd52b88781478f60f00300dc6989
|
|
7
|
+
data.tar.gz: 85917710e87e7cec29e5b5226a1e123a7d01619c1b61f44a7029ee95d321122f20eb6580204cfe364899ab5b14449b2e544c4439b8d18860f85d89902c02078c
|
data/.gitignore
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# Ignore bundler config
|
|
2
|
+
.bundle
|
|
3
|
+
|
|
4
|
+
# Ignore the default SQLite database.
|
|
5
|
+
/db/*.sqlite3
|
|
6
|
+
|
|
7
|
+
# Ignore all logfiles and tempfiles.
|
|
8
|
+
/log/*.log
|
|
9
|
+
/tmp
|
|
10
|
+
spec/reports
|
|
11
|
+
test/tmp
|
|
12
|
+
test/version_tmp
|
|
13
|
+
|
|
14
|
+
/public/assets
|
|
15
|
+
/public/uploads
|
|
16
|
+
/public/system
|
|
17
|
+
/backups
|
|
18
|
+
/doc
|
|
19
|
+
lib/bundler/man
|
|
20
|
+
*.pid
|
|
21
|
+
*.sql
|
|
22
|
+
*.swp
|
|
23
|
+
*.swo
|
|
24
|
+
*~
|
|
25
|
+
*.bak
|
|
26
|
+
*.log
|
|
27
|
+
*.tar.bz2
|
|
28
|
+
*.tar.gz
|
|
29
|
+
*.zip
|
|
30
|
+
*.gem
|
|
31
|
+
*.rbc
|
|
32
|
+
.settings
|
|
33
|
+
.yardoc
|
|
34
|
+
.svn
|
|
35
|
+
.project
|
|
36
|
+
|
|
37
|
+
nohub.out
|
|
38
|
+
InstalledFiles
|
|
39
|
+
_yardoc
|
|
40
|
+
coverage
|
|
41
|
+
pkg
|
|
42
|
+
rdoc
|
data/CHANGELOG.rdoc
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
PATH
|
|
2
|
+
remote: .
|
|
3
|
+
specs:
|
|
4
|
+
mn_model (0.0.1)
|
|
5
|
+
activerecord (= 4.0.0.beta1)
|
|
6
|
+
pg
|
|
7
|
+
|
|
8
|
+
GEM
|
|
9
|
+
remote: https://rubygems.org/
|
|
10
|
+
specs:
|
|
11
|
+
activemodel (4.0.0.beta1)
|
|
12
|
+
activesupport (= 4.0.0.beta1)
|
|
13
|
+
builder (~> 3.1.0)
|
|
14
|
+
activerecord (4.0.0.beta1)
|
|
15
|
+
activemodel (= 4.0.0.beta1)
|
|
16
|
+
activerecord-deprecated_finders (~> 0.0.3)
|
|
17
|
+
activesupport (= 4.0.0.beta1)
|
|
18
|
+
arel (~> 4.0.0.beta1)
|
|
19
|
+
activerecord-deprecated_finders (0.0.3)
|
|
20
|
+
activesupport (4.0.0.beta1)
|
|
21
|
+
i18n (~> 0.6.2)
|
|
22
|
+
minitest (~> 4.2)
|
|
23
|
+
multi_json (~> 1.3)
|
|
24
|
+
thread_safe (~> 0.1)
|
|
25
|
+
tzinfo (~> 0.3.33)
|
|
26
|
+
arel (4.0.0)
|
|
27
|
+
atomic (1.1.8)
|
|
28
|
+
builder (3.1.4)
|
|
29
|
+
coderay (1.0.9)
|
|
30
|
+
ffi (1.7.0)
|
|
31
|
+
formatador (0.2.4)
|
|
32
|
+
guard (1.7.0)
|
|
33
|
+
formatador (>= 0.2.4)
|
|
34
|
+
listen (>= 0.6.0)
|
|
35
|
+
lumberjack (>= 1.0.2)
|
|
36
|
+
pry (>= 0.9.10)
|
|
37
|
+
thor (>= 0.14.6)
|
|
38
|
+
guard-minitest (0.5.0)
|
|
39
|
+
guard (>= 0.4)
|
|
40
|
+
i18n (0.6.4)
|
|
41
|
+
listen (0.7.3)
|
|
42
|
+
lumberjack (1.0.3)
|
|
43
|
+
method_source (0.8.1)
|
|
44
|
+
minitest (4.7.2)
|
|
45
|
+
multi_json (1.7.2)
|
|
46
|
+
pg (0.15.1)
|
|
47
|
+
pry (0.9.12)
|
|
48
|
+
coderay (~> 1.0.5)
|
|
49
|
+
method_source (~> 0.8)
|
|
50
|
+
slop (~> 3.4)
|
|
51
|
+
rake (10.0.4)
|
|
52
|
+
rb-inotify (0.9.0)
|
|
53
|
+
ffi (>= 0.5.0)
|
|
54
|
+
slop (3.4.4)
|
|
55
|
+
thor (0.18.1)
|
|
56
|
+
thread_safe (0.1.0)
|
|
57
|
+
atomic
|
|
58
|
+
tzinfo (0.3.37)
|
|
59
|
+
|
|
60
|
+
PLATFORMS
|
|
61
|
+
ruby
|
|
62
|
+
|
|
63
|
+
DEPENDENCIES
|
|
64
|
+
bundler
|
|
65
|
+
guard-minitest
|
|
66
|
+
mn_model!
|
|
67
|
+
pry
|
|
68
|
+
rake
|
|
69
|
+
rb-inotify
|
data/Guardfile
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# A sample Guardfile
|
|
2
|
+
# More info at https://github.com/guard/guard#readme
|
|
3
|
+
|
|
4
|
+
guard 'minitest' do
|
|
5
|
+
# with Minitest::Unit
|
|
6
|
+
watch(%r|^test/(.*)\/?test_(.*)\.rb|)
|
|
7
|
+
watch(%r|^lib/(.*)([^/]+)\.rb|) { |m| "test/#{m[1]}test_#{m[2]}.rb" }
|
|
8
|
+
watch(%r|^test/test_helper\.rb|) { "test" }
|
|
9
|
+
|
|
10
|
+
# with Minitest::Spec
|
|
11
|
+
# watch(%r|^spec/(.*)_spec\.rb|)
|
|
12
|
+
# watch(%r|^lib/(.*)([^/]+)\.rb|) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
|
|
13
|
+
# watch(%r|^spec/spec_helper\.rb|) { "spec" }
|
|
14
|
+
|
|
15
|
+
# Rails 3.2
|
|
16
|
+
# watch(%r|^app/controllers/(.*)\.rb|) { |m| "test/controllers/#{m[1]}_test.rb" }
|
|
17
|
+
# watch(%r|^app/helpers/(.*)\.rb|) { |m| "test/helpers/#{m[1]}_test.rb" }
|
|
18
|
+
# watch(%r|^app/models/(.*)\.rb|) { |m| "test/unit/#{m[1]}_test.rb" }
|
|
19
|
+
|
|
20
|
+
# Rails
|
|
21
|
+
# watch(%r|^app/controllers/(.*)\.rb|) { |m| "test/functional/#{m[1]}_test.rb" }
|
|
22
|
+
# watch(%r|^app/helpers/(.*)\.rb|) { |m| "test/helpers/#{m[1]}_test.rb" }
|
|
23
|
+
# watch(%r|^app/models/(.*)\.rb|) { |m| "test/unit/#{m[1]}_test.rb" }
|
|
24
|
+
end
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
Copyright (c) 2013 Andersen Fan
|
|
2
|
+
|
|
3
|
+
MIT License
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
6
|
+
a copy of this software and associated documentation files (the
|
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
11
|
+
the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be
|
|
14
|
+
included in all copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# MnModel
|
|
2
|
+
|
|
3
|
+
TODO: Write a gem description
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Add this line to your application's Gemfile:
|
|
8
|
+
|
|
9
|
+
gem 'mn_model'
|
|
10
|
+
|
|
11
|
+
And then execute:
|
|
12
|
+
|
|
13
|
+
$ bundle
|
|
14
|
+
|
|
15
|
+
Or install it yourself as:
|
|
16
|
+
|
|
17
|
+
$ gem install mn_model
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
TODO: Write usage instructions here
|
|
22
|
+
|
|
23
|
+
## Contributing
|
|
24
|
+
|
|
25
|
+
1. Fork it
|
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
require "bundler/setup"
|
|
2
|
+
require "bundler/gem_tasks"
|
|
3
|
+
require "rake/testtask"
|
|
4
|
+
require 'pg'
|
|
5
|
+
require 'active_record'
|
|
6
|
+
require 'yaml'
|
|
7
|
+
|
|
8
|
+
Rake::TestTask.new do |t|
|
|
9
|
+
t.libs << "test"
|
|
10
|
+
t.test_files = FileList["test/**/test*.rb"]
|
|
11
|
+
t.verbose = true
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
desc "Run tests"
|
|
15
|
+
task default: :test
|
|
16
|
+
|
|
17
|
+
namespace :db do
|
|
18
|
+
db_configurations = YAML::load(File.open('config/database.yml'))
|
|
19
|
+
|
|
20
|
+
desc "Migrate the db"
|
|
21
|
+
task :migrate do
|
|
22
|
+
ActiveRecord::Base.establish_connection(db_configurations)
|
|
23
|
+
ActiveRecord::Migrator.migrate("db/migrate/")
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
desc "Create the db"
|
|
27
|
+
task :create do
|
|
28
|
+
admin_connection = db_configurations.merge({'database'=> 'postgres',
|
|
29
|
+
'schema_search_path'=> 'public'})
|
|
30
|
+
ActiveRecord::Base.establish_connection(admin_connection)
|
|
31
|
+
ActiveRecord::Base.connection.create_database(db_configurations.fetch('database'))
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
desc "drop the db"
|
|
35
|
+
task :drop do
|
|
36
|
+
admin_connection = db_configurations.merge({'database'=> 'postgres',
|
|
37
|
+
'schema_search_path'=> 'public'})
|
|
38
|
+
ActiveRecord::Base.establish_connection(admin_connection)
|
|
39
|
+
ActiveRecord::Base.connection.drop_database(db_configurations.fetch('database'))
|
|
40
|
+
end
|
|
41
|
+
end
|
data/config/database.yml
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
class CreateNotes < ActiveRecord::Migration
|
|
2
|
+
def change
|
|
3
|
+
create_table :notes do |t|
|
|
4
|
+
t.text :name, null: false
|
|
5
|
+
t.text :description
|
|
6
|
+
t.timestamps
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
create_table :fields do |t|
|
|
10
|
+
t.references :note
|
|
11
|
+
t.string :name, null: false
|
|
12
|
+
t.timestamps
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
create_table :entries do |t|
|
|
16
|
+
t.references :note
|
|
17
|
+
t.timestamps
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
create_table :items do |t|
|
|
21
|
+
t.references :field
|
|
22
|
+
t.references :entry
|
|
23
|
+
t.text :content
|
|
24
|
+
t.timestamps
|
|
25
|
+
|
|
26
|
+
t.index([:field_id, :entry_id], :unique => true)
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
data/lib/mn_model.rb
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
require "bundler/setup"
|
|
2
|
+
require "yaml"
|
|
3
|
+
require "pg"
|
|
4
|
+
require "active_record"
|
|
5
|
+
require "logger"
|
|
6
|
+
require "pry"
|
|
7
|
+
|
|
8
|
+
LIB_PATH = File.dirname(File.absolute_path(__FILE__))
|
|
9
|
+
|
|
10
|
+
DB_CONFIGURATIONS = YAML::load(File.read(File.join(LIB_PATH, '../config/database.yml')))
|
|
11
|
+
ActiveRecord::Base.establish_connection(DB_CONFIGURATIONS)
|
|
12
|
+
#puts Note.superclass.connection.current_database
|
|
13
|
+
ActiveRecord::Base.logger = Logger.new(File.join(LIB_PATH, '../log/db.log'), 'weekly')
|
|
14
|
+
|
|
15
|
+
Dir.glob(LIB_PATH + "/mn_model/*.rb").each {|f| require f}
|
|
16
|
+
#require "mn_model/version"
|
|
17
|
+
#require "mn_model/note"
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
module MnModel
|
|
21
|
+
# Your code goes here...
|
|
22
|
+
end
|
|
23
|
+
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
module MnModel
|
|
2
|
+
class Entry < ActiveRecord::Base
|
|
3
|
+
establish_connection DB_CONFIGURATIONS
|
|
4
|
+
|
|
5
|
+
belongs_to :note
|
|
6
|
+
has_many :items
|
|
7
|
+
|
|
8
|
+
class << self
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def with_data
|
|
12
|
+
with_data = {"data" => Hash.new}.merge! self.serializable_hash
|
|
13
|
+
note.fields.each do |f|
|
|
14
|
+
item = Item.where(entry_id: id, field_id: f.id).first
|
|
15
|
+
with_data["data"].merge!(f.name => item.content) if item
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
return with_data
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
module MnModel
|
|
2
|
+
class Note < ActiveRecord::Base
|
|
3
|
+
establish_connection DB_CONFIGURATIONS
|
|
4
|
+
|
|
5
|
+
has_many :fields
|
|
6
|
+
has_many :entries
|
|
7
|
+
has_many :items, through: :fields
|
|
8
|
+
|
|
9
|
+
validates_presence_of :name
|
|
10
|
+
|
|
11
|
+
def create_entry_with_data(attributes={})
|
|
12
|
+
entry_with_data = {"data" => Hash.new}
|
|
13
|
+
|
|
14
|
+
#transaction do
|
|
15
|
+
#end
|
|
16
|
+
entry = Entry.create note_id: id
|
|
17
|
+
entry_with_data.merge! entry.serializable_hash
|
|
18
|
+
|
|
19
|
+
fields.each do |f|
|
|
20
|
+
#if attributes[f.name].present?
|
|
21
|
+
unless attributes[f.name].nil?
|
|
22
|
+
item = entry.items.create field_id: f.id, content: attributes[f.name]
|
|
23
|
+
entry_with_data["data"].merge!(f.name => item.content)
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
return entry_with_data
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def find_entry_with_data(options={})
|
|
31
|
+
entry = Entry.find options["entry_id"]
|
|
32
|
+
entry_with_data = {"data" => Hash.new}.merge! entry.serializable_hash
|
|
33
|
+
|
|
34
|
+
fields.each do |f|
|
|
35
|
+
item = Item.where(entry_id: entry.id, field_id: f.id).first
|
|
36
|
+
#entry_with_data["data"].merge!(f.name => item.try(:content))
|
|
37
|
+
entry_with_data["data"].merge!(f.name => item.content) if item
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
return entry_with_data
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def all_entries_with_data(options={})
|
|
44
|
+
#entries_with_date = get_entries_with_data_by_sql(entries)
|
|
45
|
+
entries_with_date = get_entries_with_data_by_cal(items_with_field_name)
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def items_with_field_name
|
|
50
|
+
items.select("items.*, fields.name as field_name")
|
|
51
|
+
end
|
|
52
|
+
private
|
|
53
|
+
|
|
54
|
+
def get_entries_with_data_by_sql(selected_entries)
|
|
55
|
+
entries_with_date = selected_entries.collect do |entry|
|
|
56
|
+
entry_with_data = {"data" => Hash.new}.merge! entry.serializable_hash
|
|
57
|
+
|
|
58
|
+
fields.each do |f|
|
|
59
|
+
item = Item.where(entry_id: entry.id, field_id: f.id).first
|
|
60
|
+
#entry_with_data["data"].merge!(f.name => item.try(:content))
|
|
61
|
+
entry_with_data["data"].merge!(f.name => item.content) if item
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
entry_with_data
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def get_entries_with_data_by_cal(items)
|
|
69
|
+
entries_with_date = items.group_by{|e| e.entry_id.to_s}.map{|k, v| {"entry_id" => k, "data" => v.inject({}){|h, e| h.merge(e.field_name => e.content)}}}
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
end
|
data/mn_model.gemspec
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
+
require 'mn_model/version'
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |spec|
|
|
7
|
+
spec.name = "mn_model"
|
|
8
|
+
spec.version = MnModel::VERSION
|
|
9
|
+
spec.authors = ["Andersen Fan"]
|
|
10
|
+
spec.email = ["as181920@hotmail.com"]
|
|
11
|
+
spec.description = %q{model for micro_notes}
|
|
12
|
+
spec.summary = %q{basic data logic}
|
|
13
|
+
spec.homepage = ""
|
|
14
|
+
spec.license = "MIT"
|
|
15
|
+
|
|
16
|
+
spec.files = `git ls-files`.split($/)
|
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
|
19
|
+
spec.require_paths = ["lib"]
|
|
20
|
+
|
|
21
|
+
spec.add_dependency "pg"
|
|
22
|
+
spec.add_dependency 'activerecord', "4.0.0.beta1"#,"~>3.2.12"
|
|
23
|
+
#spec.add_dependency 'activerecord', "~>3.2.12"
|
|
24
|
+
|
|
25
|
+
spec.add_development_dependency "bundler" #, "~> 1.3"
|
|
26
|
+
spec.add_development_dependency "rake"
|
|
27
|
+
#spec.add_development_dependency "pg"
|
|
28
|
+
#spec.add_development_dependency "factory_girl"
|
|
29
|
+
spec.add_development_dependency "guard-minitest"
|
|
30
|
+
spec.add_development_dependency "rb-inotify"
|
|
31
|
+
end
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
require "minitest/autorun"
|
|
2
|
+
require "mn_model"
|
|
3
|
+
|
|
4
|
+
include MnModel
|
|
5
|
+
|
|
6
|
+
describe MnModel do
|
|
7
|
+
before do
|
|
8
|
+
Note.destroy_all
|
|
9
|
+
Field.destroy_all
|
|
10
|
+
Entry.destroy_all
|
|
11
|
+
Item.destroy_all
|
|
12
|
+
@note = Note.create name: "name_for_test"
|
|
13
|
+
@note.fields.create name: "test_field_1"
|
|
14
|
+
@note.fields.create name: "test_field_2"
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
describe "CRUD for entries" do
|
|
18
|
+
it "one note have no entries by default" do
|
|
19
|
+
@note.entries.must_be_empty
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
it "one note have note entries and data" do
|
|
23
|
+
all_entries_with_data = @note.all_entries_with_data()
|
|
24
|
+
all_entries_with_data.must_be_instance_of Array
|
|
25
|
+
all_entries_with_data.must_be_empty
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
it "can create entries with data for a note and get them back" do
|
|
29
|
+
field_1_name, field_2_name = @note.fields[0].name, @note.fields[1].name
|
|
30
|
+
field_1_content, field_2_content = "c1", "c2"
|
|
31
|
+
|
|
32
|
+
entry_with_data = @note.create_entry_with_data field_1_name => field_1_content, field_2_name => field_2_content
|
|
33
|
+
entry_with_data.must_be_instance_of Hash
|
|
34
|
+
@note.entries.count.must_equal 1
|
|
35
|
+
entry_with_data["data"][field_1_name].must_equal field_1_content
|
|
36
|
+
entry_with_data["data"][field_2_name].must_equal field_2_content
|
|
37
|
+
field_1_content, field_2_content = "c3", "c4"
|
|
38
|
+
entry_with_data = @note.create_entry_with_data field_1_name => field_1_content, field_2_name => field_2_content
|
|
39
|
+
@note.entries.count.must_equal 2
|
|
40
|
+
entry_with_data["data"][field_1_name].must_equal field_1_content
|
|
41
|
+
entry_with_data["data"][field_2_name].must_equal field_2_content
|
|
42
|
+
|
|
43
|
+
# get entry with data by entry_id
|
|
44
|
+
entry_with_data = @note.find_entry_with_data "entry_id" => entry_with_data["id"]
|
|
45
|
+
entry_with_data.must_be_instance_of Hash
|
|
46
|
+
entry_with_data["data"][field_1_name].must_equal field_1_content
|
|
47
|
+
|
|
48
|
+
# get etnry with data by entry
|
|
49
|
+
entry_with_data = Entry.find(entry_with_data["id"]).with_data
|
|
50
|
+
entry_with_data.must_be_instance_of Hash
|
|
51
|
+
entry_with_data["data"][field_1_name].must_equal field_1_content
|
|
52
|
+
|
|
53
|
+
# get all entries
|
|
54
|
+
# 1000.times {all_entries_with_data = @note.all_entries_with_data}
|
|
55
|
+
all_entries_with_data = @note.all_entries_with_data
|
|
56
|
+
all_entries_with_data.must_be_instance_of Array
|
|
57
|
+
all_entries_with_data.length.must_equal 2
|
|
58
|
+
all_entries_with_data.last["data"][field_1_name].must_equal field_1_content
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
it "can find entry with data by a entry" do
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
it "created entry will ignore unknown field data" do
|
|
65
|
+
field_1_name, field_2_name, field_unknown = @note.fields[0].name, @note.fields[1].name, "unknown_field"
|
|
66
|
+
field_1_content, field_2_content, field_unknown_content = "c1", "c2", "?"
|
|
67
|
+
|
|
68
|
+
entry_with_data = @note.create_entry_with_data field_1_name => field_1_content, field_2_name => field_2_content, field_unknown => field_unknown_content
|
|
69
|
+
@note.entries.count.must_equal 1
|
|
70
|
+
@note.items.count.must_equal 2
|
|
71
|
+
entry_with_data["data"][field_1_name].must_equal field_1_content
|
|
72
|
+
entry_with_data["data"][field_unknown].must_be_nil
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
it "created entry will ignore unsetted field and unknown fields" do
|
|
76
|
+
field_1_name, field_2_name, field_unknown = @note.fields[0].name, @note.fields[1].name, "unknown_field"
|
|
77
|
+
field_1_content = "c1"
|
|
78
|
+
|
|
79
|
+
entry_with_data = @note.create_entry_with_data field_1_name => field_1_content, field_unknown => "?"
|
|
80
|
+
@note.entries.count.must_equal 1
|
|
81
|
+
@note.items.count.must_equal 1
|
|
82
|
+
entry_with_data["data"].keys.must_include field_1_name
|
|
83
|
+
entry_with_data["data"][field_1_name].must_equal field_1_content
|
|
84
|
+
entry_with_data["data"].keys.wont_include field_2_name
|
|
85
|
+
entry_with_data["data"].keys.wont_include field_unknown
|
|
86
|
+
|
|
87
|
+
entry_with_data = @note.find_entry_with_data "entry_id" => entry_with_data["id"]
|
|
88
|
+
entry_with_data["data"].keys.must_include field_1_name
|
|
89
|
+
entry_with_data["data"][field_1_name].must_equal field_1_content
|
|
90
|
+
entry_with_data["data"].keys.wont_include field_2_name
|
|
91
|
+
entry_with_data["data"].keys.wont_include field_unknown
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
it "can create entry will field which value is empty" do
|
|
95
|
+
field_1_name, field_2_name = @note.fields[0].name, @note.fields[1].name
|
|
96
|
+
entry_with_data = @note.create_entry_with_data field_1_name => "x", field_2_name => ""
|
|
97
|
+
entry_with_data["data"].keys.must_include field_1_name
|
|
98
|
+
entry_with_data["data"].keys.must_include field_2_name
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
describe "advanced query for entries" do
|
|
104
|
+
before do
|
|
105
|
+
#Note.destroy_all
|
|
106
|
+
@note = Note.create name: "name_for_test"
|
|
107
|
+
@note.fields.create name: "test_field_1"
|
|
108
|
+
@note.fields.create name: "test_field_2"
|
|
109
|
+
|
|
110
|
+
@field_1_name, @field_2_name = @note.fields[0].name, @note.fields[1].name
|
|
111
|
+
5.times do |i|
|
|
112
|
+
@note.create_entry_with_data @field_1_name => "field_1_content_#{i}", @field_2_name => "field_2_content_#{i}"
|
|
113
|
+
end
|
|
114
|
+
6.times do |i|
|
|
115
|
+
@note.create_entry_with_data @field_1_name => "field_1_ccc_#{i}", @field_2_name => "field_2_ccc_#{i}"
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
it "can get entries by conditions" do
|
|
120
|
+
#TODO
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
end
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
require "minitest/autorun"
|
|
2
|
+
require "mn_model"
|
|
3
|
+
|
|
4
|
+
include MnModel
|
|
5
|
+
|
|
6
|
+
describe MnModel do
|
|
7
|
+
before do
|
|
8
|
+
Note.destroy_all
|
|
9
|
+
Field.destroy_all
|
|
10
|
+
@note = Note.create name: "name_for_test"
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
describe "CRUD for Fields" do
|
|
14
|
+
it "one note have no fields by default" do
|
|
15
|
+
@note.fields.must_be_empty
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
it "can CRUD fields for one note" do
|
|
19
|
+
name = "name_for_field"
|
|
20
|
+
field = @note.fields.create name: name
|
|
21
|
+
@note.fields.count.must_equal 1
|
|
22
|
+
field.name.must_equal name
|
|
23
|
+
@note.fields.create name: "another field"
|
|
24
|
+
@note.fields.count.must_equal 2
|
|
25
|
+
|
|
26
|
+
field = @note.fields.find field.id
|
|
27
|
+
field.wont_be_nil
|
|
28
|
+
|
|
29
|
+
name_modified = "name_modified"
|
|
30
|
+
field.name = name_modified
|
|
31
|
+
field.save
|
|
32
|
+
field.name.must_equal name_modified
|
|
33
|
+
field.name = name
|
|
34
|
+
@note.save
|
|
35
|
+
field.name.must_equal name
|
|
36
|
+
field.name = name
|
|
37
|
+
|
|
38
|
+
field.destroy
|
|
39
|
+
proc {@note.fields.find(field.id)}.must_raise ActiveRecord::RecordNotFound
|
|
40
|
+
@note.fields.count.must_equal 1
|
|
41
|
+
@note.fields.destroy_all
|
|
42
|
+
@note.fields.must_be_empty
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
it "name should not empty" do
|
|
46
|
+
proc {@note.fields.create!}.must_raise ActiveRecord::RecordInvalid
|
|
47
|
+
field = @note.fields.create
|
|
48
|
+
field.valid?.must_equal false
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
end
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
require "minitest/autorun"
|
|
2
|
+
require "mn_model"
|
|
3
|
+
|
|
4
|
+
include MnModel
|
|
5
|
+
|
|
6
|
+
describe MnModel do
|
|
7
|
+
before do
|
|
8
|
+
#Note.destroy_all
|
|
9
|
+
#Field.destroy_all
|
|
10
|
+
Item.destroy_all
|
|
11
|
+
#@note = Note.create name: "name_for_test"
|
|
12
|
+
#@note.fields.create name: "test_field_1"
|
|
13
|
+
@entry_id = 1
|
|
14
|
+
@field_id = 11
|
|
15
|
+
@content = 'x'
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
describe "CRUD for Item" do
|
|
19
|
+
it "must have no items by default" do
|
|
20
|
+
Item.all.must_be_empty
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
it "can CRUD item" do
|
|
24
|
+
|
|
25
|
+
item = Item.create entry_id: @entry_id,field_id: @field_id,content: @content
|
|
26
|
+
Item.all.count.must_equal 1
|
|
27
|
+
item.entry_id.must_equal @entry_id
|
|
28
|
+
item.content.must_equal @content
|
|
29
|
+
|
|
30
|
+
item = Item.find item.id
|
|
31
|
+
item.wont_be_nil
|
|
32
|
+
|
|
33
|
+
content_modified = "xx"
|
|
34
|
+
item.content = content_modified
|
|
35
|
+
item.save
|
|
36
|
+
item.content.must_equal content_modified
|
|
37
|
+
|
|
38
|
+
item.destroy
|
|
39
|
+
Item.all.must_be_empty
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
it "entry_id and field_id and content should not empty" do
|
|
43
|
+
proc {Item.create!}.must_raise ActiveRecord::RecordInvalid
|
|
44
|
+
proc {Item.create!(field_id: @field_id,entry_id: @entry_id)}.must_raise ActiveRecord::RecordInvalid
|
|
45
|
+
proc {Item.create!(field_id: @field_id,content: @content)}.must_raise ActiveRecord::RecordInvalid
|
|
46
|
+
proc {Item.create!(entry_id: @entry_id,content: @content)}.must_raise ActiveRecord::RecordInvalid
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
end
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
require "minitest/autorun"
|
|
2
|
+
require "mn_model"
|
|
3
|
+
|
|
4
|
+
include MnModel
|
|
5
|
+
|
|
6
|
+
describe MnModel do
|
|
7
|
+
before do
|
|
8
|
+
Note.destroy_all
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
describe "CRUD for Note" do
|
|
12
|
+
it "must have no notes by default" do
|
|
13
|
+
Note.all.must_be_empty
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
it "can CRUD note with name and description" do
|
|
17
|
+
name, desc = "testnote1", "the note is used for testing"
|
|
18
|
+
note = Note.create name: name, description: desc
|
|
19
|
+
Note.all.count.must_equal 1
|
|
20
|
+
note.name.must_equal name
|
|
21
|
+
note.description.must_equal desc
|
|
22
|
+
|
|
23
|
+
note = Note.find note.id
|
|
24
|
+
note.wont_be_nil
|
|
25
|
+
|
|
26
|
+
name_modified = "testnote2"
|
|
27
|
+
note.name = name_modified
|
|
28
|
+
note.save
|
|
29
|
+
note.name.must_equal name_modified
|
|
30
|
+
|
|
31
|
+
note.destroy
|
|
32
|
+
Note.all.must_be_empty
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
it "name should not empty" do
|
|
36
|
+
proc {Note.create!}.must_raise ActiveRecord::RecordInvalid
|
|
37
|
+
note = Note.create
|
|
38
|
+
note.valid?.must_equal false
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: mn_model
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Andersen Fan
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2013-04-26 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: pg
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - '>='
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '0'
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - '>='
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '0'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: activerecord
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - '='
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: 4.0.0.beta1
|
|
34
|
+
type: :runtime
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - '='
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: 4.0.0.beta1
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: bundler
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - '>='
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '0'
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - '>='
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '0'
|
|
55
|
+
- !ruby/object:Gem::Dependency
|
|
56
|
+
name: rake
|
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - '>='
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: '0'
|
|
62
|
+
type: :development
|
|
63
|
+
prerelease: false
|
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - '>='
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: '0'
|
|
69
|
+
- !ruby/object:Gem::Dependency
|
|
70
|
+
name: guard-minitest
|
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
|
72
|
+
requirements:
|
|
73
|
+
- - '>='
|
|
74
|
+
- !ruby/object:Gem::Version
|
|
75
|
+
version: '0'
|
|
76
|
+
type: :development
|
|
77
|
+
prerelease: false
|
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
79
|
+
requirements:
|
|
80
|
+
- - '>='
|
|
81
|
+
- !ruby/object:Gem::Version
|
|
82
|
+
version: '0'
|
|
83
|
+
- !ruby/object:Gem::Dependency
|
|
84
|
+
name: rb-inotify
|
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
|
86
|
+
requirements:
|
|
87
|
+
- - '>='
|
|
88
|
+
- !ruby/object:Gem::Version
|
|
89
|
+
version: '0'
|
|
90
|
+
type: :development
|
|
91
|
+
prerelease: false
|
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
93
|
+
requirements:
|
|
94
|
+
- - '>='
|
|
95
|
+
- !ruby/object:Gem::Version
|
|
96
|
+
version: '0'
|
|
97
|
+
description: model for micro_notes
|
|
98
|
+
email:
|
|
99
|
+
- as181920@hotmail.com
|
|
100
|
+
executables: []
|
|
101
|
+
extensions: []
|
|
102
|
+
extra_rdoc_files: []
|
|
103
|
+
files:
|
|
104
|
+
- .gitignore
|
|
105
|
+
- CHANGELOG.rdoc
|
|
106
|
+
- Gemfile
|
|
107
|
+
- Gemfile.lock
|
|
108
|
+
- Guardfile
|
|
109
|
+
- LICENSE.txt
|
|
110
|
+
- README.md
|
|
111
|
+
- Rakefile
|
|
112
|
+
- config/database.yml
|
|
113
|
+
- db/migrate/0001_create_notes.rb
|
|
114
|
+
- lib/mn_model.rb
|
|
115
|
+
- lib/mn_model/entry.rb
|
|
116
|
+
- lib/mn_model/field.rb
|
|
117
|
+
- lib/mn_model/item.rb
|
|
118
|
+
- lib/mn_model/note.rb
|
|
119
|
+
- lib/mn_model/version.rb
|
|
120
|
+
- mn_model.gemspec
|
|
121
|
+
- test/mn_model/test_entry.rb
|
|
122
|
+
- test/mn_model/test_field.rb
|
|
123
|
+
- test/mn_model/test_item.rb
|
|
124
|
+
- test/mn_model/test_note.rb
|
|
125
|
+
homepage: ''
|
|
126
|
+
licenses:
|
|
127
|
+
- MIT
|
|
128
|
+
metadata: {}
|
|
129
|
+
post_install_message:
|
|
130
|
+
rdoc_options: []
|
|
131
|
+
require_paths:
|
|
132
|
+
- lib
|
|
133
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
134
|
+
requirements:
|
|
135
|
+
- - '>='
|
|
136
|
+
- !ruby/object:Gem::Version
|
|
137
|
+
version: '0'
|
|
138
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
139
|
+
requirements:
|
|
140
|
+
- - '>='
|
|
141
|
+
- !ruby/object:Gem::Version
|
|
142
|
+
version: '0'
|
|
143
|
+
requirements: []
|
|
144
|
+
rubyforge_project:
|
|
145
|
+
rubygems_version: 2.0.0.rc.2
|
|
146
|
+
signing_key:
|
|
147
|
+
specification_version: 4
|
|
148
|
+
summary: basic data logic
|
|
149
|
+
test_files:
|
|
150
|
+
- test/mn_model/test_entry.rb
|
|
151
|
+
- test/mn_model/test_field.rb
|
|
152
|
+
- test/mn_model/test_item.rb
|
|
153
|
+
- test/mn_model/test_note.rb
|