unidom-common 0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +18 -0
- data/Rakefile +24 -0
- data/app/assets/javascripts/unidom/common/application.js +13 -0
- data/app/assets/stylesheets/unidom/common/application.css +15 -0
- data/app/controllers/unidom/common/application_controller.rb +2 -0
- data/app/helpers/unidom/common/application_helper.rb +2 -0
- data/app/models/unidom/common/concerns/model_extension.rb +129 -0
- data/app/views/layouts/unidom/common/application.html.erb +14 -0
- data/config/routes.rb +2 -0
- data/db/migrate/20000101000000_enable_uuid_extension.rb +7 -0
- data/lib/tasks/common_tasks.rake +4 -0
- data/lib/unidom/common/data_helper.rb +33 -0
- data/lib/unidom/common/engine.rb +26 -0
- data/lib/unidom/common/version.rb +5 -0
- data/lib/unidom/common.rb +7 -0
- metadata +117 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 45cf624b211946c52147a9822064f2595275a35a
|
4
|
+
data.tar.gz: 2395a2969d1650d45f53d3ba88e389729f375e9e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 671a636ce38f60174a94b7a2877ecb3f75b03d31d6d388b5541cdbb8b780e72d1f8c7e8e0b164adcde148962a5bad30b2a8dd1eded3e77d8b07467c387e99a44
|
7
|
+
data.tar.gz: e03c9f872b63da3ef19a647dd3959241a84e6d57bac161c45dfd7e5239784cbc820b4402e47ef7c52cb89fc2e34dc1d2d3b4730b49c6a9e933924b101f2724ce
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2015 Topbit Du
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# Unidom Common
|
2
|
+
|
3
|
+
Unidom (UNIfied Domain Object Model) is a series of domain model engines. The Common domain model engine includes the common models.
|
4
|
+
Unidom (统一领域对象模型)是一系列的领域模型引擎。常用领域模型引擎包括一些常用的模型。
|
5
|
+
|
6
|
+
## Usage in Gemfile:
|
7
|
+
```ruby
|
8
|
+
gem 'unidom-common'
|
9
|
+
```
|
10
|
+
## Run the Database Migration:
|
11
|
+
```shell
|
12
|
+
rake db:migrate
|
13
|
+
```
|
14
|
+
|
15
|
+
## Include Concern in Models:
|
16
|
+
```ruby
|
17
|
+
include Unidom::Common::Concerns::ModelExtension
|
18
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'Unidom::Common'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.rdoc')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
load 'rails/tasks/statistics.rake'
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
Bundler::GemHelper.install_tasks
|
24
|
+
|
@@ -0,0 +1,13 @@
|
|
1
|
+
// This is a manifest file that'll be compiled into application.js, which will include all the files
|
2
|
+
// listed below.
|
3
|
+
//
|
4
|
+
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
|
5
|
+
// or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
|
6
|
+
//
|
7
|
+
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
|
8
|
+
// compiled file.
|
9
|
+
//
|
10
|
+
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
|
11
|
+
// about supported directives.
|
12
|
+
//
|
13
|
+
//= require_tree .
|
@@ -0,0 +1,15 @@
|
|
1
|
+
/*
|
2
|
+
* This is a manifest file that'll be compiled into application.css, which will include all the files
|
3
|
+
* listed below.
|
4
|
+
*
|
5
|
+
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
|
6
|
+
* or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
|
7
|
+
*
|
8
|
+
* You're free to add application-wide styles to this file and they'll appear at the bottom of the
|
9
|
+
* compiled file so the styles you add here take precedence over styles defined in any styles
|
10
|
+
* defined in the other CSS/SCSS files in this directory. It is generally better to create a new
|
11
|
+
* file per style scope.
|
12
|
+
*
|
13
|
+
*= require_tree .
|
14
|
+
*= require_self
|
15
|
+
*/
|
@@ -0,0 +1,129 @@
|
|
1
|
+
module Unidom
|
2
|
+
module Common
|
3
|
+
module Concerns
|
4
|
+
|
5
|
+
module ModelExtension
|
6
|
+
|
7
|
+
extend ActiveSupport::Concern
|
8
|
+
|
9
|
+
self.included do |includer|
|
10
|
+
|
11
|
+
validates :state, presence: true, length: { is: columns_hash['state'].limit }
|
12
|
+
|
13
|
+
scope :included_by, ->(inclusion) { where id: inclusion }
|
14
|
+
scope :excluded_by, ->(exclusion) { where.not id: exclusion }
|
15
|
+
|
16
|
+
scope :transited_to, ->(states) { where state: states }
|
17
|
+
|
18
|
+
scope :valid_at, ->(now = Time.now) { where "? BETWEEN #{includer.table_name}.opened_at AND #{includer.table_name}.closed_at", now }
|
19
|
+
scope :valid_duration, ->(range) { where "(#{includer.table_name}.opened_at BETWEEN ? AND ?) OR (#{includer.table_name}.closed_at <= ? AND #{includer.table_name}.closed_at >= ?)", range.min, range.max, range.max, range.min }
|
20
|
+
|
21
|
+
scope :alive, ->(living = true) { where defunct: !living }
|
22
|
+
scope :dead, ->(defunct = true) { where defunct: defunct }
|
23
|
+
|
24
|
+
if columns_hash['ordinal'].present?
|
25
|
+
validates :ordinal, presence: true, numericality: { integer_only: true, greater_than: 0 }
|
26
|
+
scope :ordinal_is, ->(ordinal) { where ordinal: ordinal }
|
27
|
+
end
|
28
|
+
|
29
|
+
if columns_hash['uuid'].present?
|
30
|
+
validates :uuid, presence: true, length: { is: 36 }
|
31
|
+
scope :uuid_is, ->(uuid) { where uuid: uuid }
|
32
|
+
end
|
33
|
+
|
34
|
+
if columns_hash['elemental'].present?
|
35
|
+
scope :primary, ->(elemental = true) { where elemental: elemental }
|
36
|
+
end
|
37
|
+
|
38
|
+
if columns_hash['grade'].present?
|
39
|
+
validates :grade, presence: true, numericality: { integer_only: true, greater_than_or_equal_to: 0 }
|
40
|
+
scope :grade_is, ->(grade) { where grade: grade }
|
41
|
+
scope :grade_higher_than, ->(grade) { where "grade > :grade", grade: grade }
|
42
|
+
scope :grade_lower_than, ->(grade) { where "grade < :grade", grade: grade }
|
43
|
+
end
|
44
|
+
|
45
|
+
if columns_hash['priority'].present?
|
46
|
+
validates :priority, presence: true, numericality: { integer_only: true, greater_than_or_equal_to: 0 }
|
47
|
+
scope :priority_is, ->(priority) { where priority: priority }
|
48
|
+
scope :priority_higher_than, ->(priority) { where "priority > :priority", priority: priority }
|
49
|
+
scope :priority_lower_than, ->(priority) { where "priority < :priority", priority: priority }
|
50
|
+
end
|
51
|
+
|
52
|
+
if columns_hash['slug'].present?
|
53
|
+
validates :slug, presence: true, length: { in: 1..columns_hash['slug'].limit }, uniqueness: true
|
54
|
+
scope :slug_is, ->(slug) { where slug: slug }
|
55
|
+
before_validation -> {
|
56
|
+
prefix = build_slug.to_s[0..(self.class.columns_hash['slug'].limit-37)]
|
57
|
+
unique = prefix
|
58
|
+
while includer.slug_is(unique).count>0
|
59
|
+
unique = "#{prefix}-#{::SecureRandom.uuid}"
|
60
|
+
end
|
61
|
+
self.slug = unique
|
62
|
+
}, on: :create
|
63
|
+
# on update: re-generate the slug if the slug was assigned to empty.
|
64
|
+
# to_query: escape characters
|
65
|
+
end
|
66
|
+
|
67
|
+
columns_hash.each do |column_name, hash|
|
68
|
+
|
69
|
+
name = column_name.to_s
|
70
|
+
|
71
|
+
if 'code'==name or name.ends_with? '_code'
|
72
|
+
class_eval do
|
73
|
+
if columns_hash[name].null
|
74
|
+
validates name.to_sym, allow_blank: true, length: { maximum: columns_hash[name].limit }
|
75
|
+
else
|
76
|
+
validates name.to_sym, presence: true, length: { is: columns_hash[name].limit }
|
77
|
+
end
|
78
|
+
scope "#{name}d_as".to_sym, ->(code) { where name => code }
|
79
|
+
scope "not_#{name}d_as".to_sym, ->(code) { where.not name => code }
|
80
|
+
scope "#{name}_starting_with".to_sym, ->(prefix) { where "#{name} LIKE :prefix", prefix: "#{prefix}%" }
|
81
|
+
scope "#{name}_ending_with".to_sym, ->(suffix) { where "#{name} LIKE :suffix", suffix: "%#{suffix}" }
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
if name.ends_with? '_at'
|
86
|
+
matched = /\A(.+)_at\z/.match name
|
87
|
+
class_eval do
|
88
|
+
scope :"#{matched[1]}_on", ->(date) { where name => date.beginning_of_day..date.end_of_day }
|
89
|
+
scope :"#{matched[1]}_during", ->(range) { where name => range }
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
end
|
94
|
+
|
95
|
+
includer.define_singleton_method :default_scope do
|
96
|
+
includer.all.order("#{includer.table_name}.ordinal ASC") if includer.columns_hash['ordinal'].present?
|
97
|
+
includer.all.order("#{includer.table_name}.created_at ASC")
|
98
|
+
#relation = base.all
|
99
|
+
#scopes.each do |s| relation = relation.send s.to_sym end
|
100
|
+
#relation
|
101
|
+
end
|
102
|
+
|
103
|
+
def soft_destroy
|
104
|
+
self.closed_at = Time.now
|
105
|
+
self.defunct = true
|
106
|
+
self.save
|
107
|
+
end
|
108
|
+
|
109
|
+
def build_slug
|
110
|
+
if respond_to? :name
|
111
|
+
name
|
112
|
+
elsif respond_to? :title
|
113
|
+
title
|
114
|
+
else
|
115
|
+
::SecureRandom.uuid
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
end
|
120
|
+
|
121
|
+
module ClassMethods
|
122
|
+
# def method_name do end
|
123
|
+
end
|
124
|
+
|
125
|
+
end
|
126
|
+
|
127
|
+
end
|
128
|
+
end
|
129
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<title>Common</title>
|
5
|
+
<%= stylesheet_link_tag 'unidom/common/application', media: 'all' %>
|
6
|
+
<%= javascript_include_tag 'unidom/common/application' %>
|
7
|
+
<%= csrf_meta_tags %>
|
8
|
+
</head>
|
9
|
+
<body>
|
10
|
+
|
11
|
+
<%= yield %>
|
12
|
+
|
13
|
+
</body>
|
14
|
+
</html>
|
data/config/routes.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'csv'
|
2
|
+
|
3
|
+
module Unidom
|
4
|
+
module Common
|
5
|
+
|
6
|
+
module DataHelper
|
7
|
+
|
8
|
+
def each_csv_row(file_name, &block)
|
9
|
+
|
10
|
+
started_at = Time.now
|
11
|
+
|
12
|
+
puts "Importing from CSV file: #{file_name}."
|
13
|
+
if file_name.blank?
|
14
|
+
puts "#{file_name} doesn't exist."
|
15
|
+
abort 1
|
16
|
+
end
|
17
|
+
|
18
|
+
CSV.foreach file_name, { encoding: 'UTF-8', headers: :first_row }, &block
|
19
|
+
|
20
|
+
puts "#{Time.now-started_at} seconds was spent to handle the given CSV."
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
def parse_time(date_text, default = Time.now)
|
25
|
+
return default if date_text.blank?
|
26
|
+
date = Date.parse date_text
|
27
|
+
Time.utc date.year, date.month, date.day
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module Unidom
|
2
|
+
module Common
|
3
|
+
|
4
|
+
class Engine < ::Rails::Engine
|
5
|
+
|
6
|
+
config.autoload_paths += %W(
|
7
|
+
#{config.root}/lib
|
8
|
+
#{config.root}/app/models/unidom/common/concerns
|
9
|
+
)
|
10
|
+
|
11
|
+
config.eager_load_paths += %W(
|
12
|
+
#{config.root}/lib
|
13
|
+
#{config.root}/app/models/unidom/common/concerns
|
14
|
+
)
|
15
|
+
|
16
|
+
isolate_namespace ::Unidom::Common
|
17
|
+
|
18
|
+
initializer :append_migrations do |app|
|
19
|
+
#app.config.paths['db/migrate'] += config.paths['db/migrate'].expanded unless app.root.to_s==root.to_s
|
20
|
+
config.paths['db/migrate'].expanded.each { |expanded_path| app.config.paths['db/migrate'] << expanded_path } unless app.root.to_s.match root.to_s
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: unidom-common
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0.2'
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Topbit Du
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-01-22 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.2'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4.2'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: pg
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.7'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.7'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
description: Unidom (UNIfied Domain Object Model) is a series of domain model engines.
|
70
|
+
The Common domain model engine includes the common models. Unidom (统一领域对象模型)是一系列的领域模型引擎。常用领域模型引擎包括一些常用的模型。
|
71
|
+
email:
|
72
|
+
- topbit.du@gmail.com
|
73
|
+
executables: []
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- MIT-LICENSE
|
78
|
+
- README.md
|
79
|
+
- Rakefile
|
80
|
+
- app/assets/javascripts/unidom/common/application.js
|
81
|
+
- app/assets/stylesheets/unidom/common/application.css
|
82
|
+
- app/controllers/unidom/common/application_controller.rb
|
83
|
+
- app/helpers/unidom/common/application_helper.rb
|
84
|
+
- app/models/unidom/common/concerns/model_extension.rb
|
85
|
+
- app/views/layouts/unidom/common/application.html.erb
|
86
|
+
- config/routes.rb
|
87
|
+
- db/migrate/20000101000000_enable_uuid_extension.rb
|
88
|
+
- lib/tasks/common_tasks.rake
|
89
|
+
- lib/unidom/common.rb
|
90
|
+
- lib/unidom/common/data_helper.rb
|
91
|
+
- lib/unidom/common/engine.rb
|
92
|
+
- lib/unidom/common/version.rb
|
93
|
+
homepage: http://github.com/topbitdu/unidom-common
|
94
|
+
licenses:
|
95
|
+
- MIT
|
96
|
+
metadata: {}
|
97
|
+
post_install_message:
|
98
|
+
rdoc_options: []
|
99
|
+
require_paths:
|
100
|
+
- lib
|
101
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - ">="
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
requirements: []
|
112
|
+
rubyforge_project:
|
113
|
+
rubygems_version: 2.4.5.1
|
114
|
+
signing_key:
|
115
|
+
specification_version: 4
|
116
|
+
summary: The Common domain model engine includes the common models.
|
117
|
+
test_files: []
|