wnm_support 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/changelog.md +11 -0
- data/db/schema.rb +7 -0
- data/lib/wnm_support/active_record_ext/destroy_validation.rb +24 -0
- data/lib/wnm_support/active_record_ext/multi_action.rb +13 -0
- data/lib/wnm_support/active_record_ext/mysql_order_by_field.rb +15 -0
- data/lib/wnm_support/active_record_ext/mysql_truncate.rb +13 -0
- data/lib/wnm_support/active_record_ext/view_helpers.rb +9 -0
- data/lib/wnm_support/version.rb +1 -1
- data/lib/wnm_support.rb +17 -0
- data/spec/spec_helper.rb +28 -0
- data/spec/wnm_support/activer_record_ext/destroy_validation_spec.rb +37 -0
- data/spec/wnm_support/activer_record_ext/multi_action_spec.rb +15 -0
- data/spec/wnm_support/activer_record_ext/mysql_order_by_filed_spec.rb +25 -0
- data/spec/wnm_support/activer_record_ext/mysql_truncate_spec.rb +21 -0
- data/spec/wnm_support/activer_record_ext/view_helpers_spec.rb +11 -0
- data/wnm_support.gemspec +3 -0
- metadata +65 -1
data/changelog.md
CHANGED
@@ -1,3 +1,14 @@
|
|
1
|
+
# 0.0.3
|
2
|
+
* add support for active record extentions
|
3
|
+
** DestroyValidation
|
4
|
+
** DestroyValidation
|
5
|
+
** ViewHelpers
|
6
|
+
** MultiAction
|
7
|
+
** MysqlTruncate
|
8
|
+
** MysqlOrderByField
|
9
|
+
* add mysql connection with one testing model
|
10
|
+
** news name:string
|
11
|
+
|
1
12
|
# 0.0.2
|
2
13
|
* rename gem to wnm_support
|
3
14
|
|
data/db/schema.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
module WnmSupport
|
2
|
+
module ActiveRecordExt
|
3
|
+
module DestroyValidation
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
included do
|
7
|
+
before_destroy :check_before_destroy
|
8
|
+
end
|
9
|
+
|
10
|
+
def can_destroy?
|
11
|
+
false
|
12
|
+
end
|
13
|
+
|
14
|
+
def check_before_destroy
|
15
|
+
if can_destroy?
|
16
|
+
true
|
17
|
+
else
|
18
|
+
errors.add(:base, "not allowed")
|
19
|
+
false
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module WnmSupport
|
2
|
+
module ActiveRecordExt
|
3
|
+
module MysqlOrderByField
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
module ClassMethods
|
7
|
+
def order_by_field(column, values = [])
|
8
|
+
return scoped if values.blank?
|
9
|
+
|
10
|
+
order("FIELD (`#{table_name}`.`#{column}`, #{values.map(&:to_i).join(", ")})")
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
data/lib/wnm_support/version.rb
CHANGED
data/lib/wnm_support.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
require "wnm_support/version"
|
2
2
|
|
3
|
+
require "active_support/concern"
|
4
|
+
|
3
5
|
require "wnm_support/core_ext/class_exists"
|
4
6
|
require "wnm_support/core_ext/to_boolean"
|
5
7
|
require "wnm_support/core_ext/to_integer"
|
@@ -8,3 +10,18 @@ require "wnm_support/core_ext/is_integer"
|
|
8
10
|
require "wnm_support/core_ext/max_words"
|
9
11
|
require "wnm_support/core_ext/map_to_hash"
|
10
12
|
require "wnm_support/core_ext/halved"
|
13
|
+
|
14
|
+
require "active_record"
|
15
|
+
require "wnm_support/active_record_ext/destroy_validation"
|
16
|
+
require "wnm_support/active_record_ext/view_helpers"
|
17
|
+
require "wnm_support/active_record_ext/multi_action"
|
18
|
+
require "wnm_support/active_record_ext/mysql_truncate"
|
19
|
+
require "wnm_support/active_record_ext/mysql_order_by_field"
|
20
|
+
|
21
|
+
class ActiveRecord::Base
|
22
|
+
include WnmSupport::ActiveRecordExt::DestroyValidation
|
23
|
+
include WnmSupport::ActiveRecordExt::ViewHelpers
|
24
|
+
include WnmSupport::ActiveRecordExt::MultiAction
|
25
|
+
include WnmSupport::ActiveRecordExt::MysqlTruncate
|
26
|
+
include WnmSupport::ActiveRecordExt::MysqlOrderByField
|
27
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,2 +1,30 @@
|
|
1
1
|
require "wnm_support"
|
2
2
|
require "active_support/all"
|
3
|
+
require "active_record_no_table"
|
4
|
+
|
5
|
+
config = HashWithIndifferentAccess.new({
|
6
|
+
:adapter => "mysql2",
|
7
|
+
:host => "127.0.0.1",
|
8
|
+
:username => "root",
|
9
|
+
:password => "",
|
10
|
+
:database => "wnm_support_test"
|
11
|
+
})
|
12
|
+
|
13
|
+
begin
|
14
|
+
ActiveRecord::Base.establish_connection(config)
|
15
|
+
rescue
|
16
|
+
ActiveRecord::Base.establish_connection(config.merge('database' => nil))
|
17
|
+
ActiveRecord::Base.connection.create_database(config['database'], :charset => 'utf8', :collation => 'utf8_unicode_ci')
|
18
|
+
end
|
19
|
+
|
20
|
+
load "db/schema.rb"
|
21
|
+
|
22
|
+
RSpec.configure do |config|
|
23
|
+
config.around do |example|
|
24
|
+
ActiveRecord::Base.transaction do
|
25
|
+
example.run
|
26
|
+
raise ActiveRecord::Rollback
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class Item < ActiveRecord::Base
|
4
|
+
include ActiveRecordNoTable
|
5
|
+
end
|
6
|
+
|
7
|
+
class News < ActiveRecord::Base
|
8
|
+
|
9
|
+
end
|
10
|
+
|
11
|
+
describe WnmSupport::ActiveRecordExt::DestroyValidation do
|
12
|
+
let(:news) { News.create!(:name => "login") }
|
13
|
+
it "should add can_destroy? to model" do
|
14
|
+
Item.new.should respond_to :can_destroy?
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should return false be default" do
|
18
|
+
Item.new.can_destroy?.should be_false
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should call can_destroy when it is destroying record" do
|
22
|
+
news.should_receive(:can_destroy?)
|
23
|
+
news.destroy
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should not destroy record when can_destroy? return false" do
|
27
|
+
news.should_receive(:can_destroy?).and_return(false)
|
28
|
+
news.destroy.should be_false
|
29
|
+
News.find_by_id(news.id).should be
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should destroy record when can_destroy? return true" do
|
33
|
+
news.should_receive(:can_destroy?).and_return(true)
|
34
|
+
news.destroy.should be_true
|
35
|
+
News.find_by_id(news.id).should be_nil
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
class Item < ActiveRecord::Base
|
4
|
+
include ActiveRecordNoTable
|
5
|
+
end
|
6
|
+
|
7
|
+
describe WnmSupport::ActiveRecordExt::MultiAction do
|
8
|
+
it "should add multi method to model" do
|
9
|
+
Item.should respond_to :multi
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should have default [:destroy]" do
|
13
|
+
Item.multi.should eq [:destroy]
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class News < ActiveRecord::Base
|
4
|
+
|
5
|
+
end
|
6
|
+
|
7
|
+
describe WnmSupport::ActiveRecordExt::MysqlOrderByField do
|
8
|
+
it "should add order_by_field method for class" do
|
9
|
+
News.should respond_to :order_by_field
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should return scope if second argument is missing or empty" do
|
13
|
+
News.order_by_field(:id).should be_kind_of ActiveRecord::Relation
|
14
|
+
News.order_by_field(:id, []).should be_kind_of ActiveRecord::Relation
|
15
|
+
News.order_by_field(:id, nil).should be_kind_of ActiveRecord::Relation
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should return records in order given by column and values" do
|
19
|
+
news_1 = News.create!(:name => "login 1")
|
20
|
+
news_2 = News.create!(:name => "login 2")
|
21
|
+
|
22
|
+
News.order_by_field(:id, [news_1.id, news_2.id]).map(&:id).should eq [news_1.id, news_2.id]
|
23
|
+
News.order_by_field(:id, [news_2.id, news_1.id]).map(&:id).should eq [news_2.id, news_1.id]
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class News < ActiveRecord::Base
|
4
|
+
|
5
|
+
end
|
6
|
+
|
7
|
+
describe WnmSupport::ActiveRecordExt::MysqlTruncate do
|
8
|
+
|
9
|
+
it "should add truncate method for class" do
|
10
|
+
News.should respond_to :truncate
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should destroy all records from database" do
|
14
|
+
News.create!(:name => "login 1")
|
15
|
+
News.create!(:name => "login 2")
|
16
|
+
|
17
|
+
News.should have(2).records
|
18
|
+
News.truncate
|
19
|
+
News.should have(0).records
|
20
|
+
end
|
21
|
+
end
|
data/wnm_support.gemspec
CHANGED
@@ -18,6 +18,9 @@ Gem::Specification.new do |gem|
|
|
18
18
|
gem.require_paths = ["lib"]
|
19
19
|
|
20
20
|
gem.add_runtime_dependency "activesupport"
|
21
|
+
gem.add_runtime_dependency "activerecord"
|
21
22
|
gem.add_development_dependency "rspec"
|
22
23
|
gem.add_development_dependency "rake"
|
24
|
+
gem.add_development_dependency "active_record_no_table"
|
25
|
+
gem.add_development_dependency "mysql2"
|
23
26
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: wnm_support
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -27,6 +27,22 @@ dependencies:
|
|
27
27
|
- - ! '>='
|
28
28
|
- !ruby/object:Gem::Version
|
29
29
|
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: activerecord
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
30
46
|
- !ruby/object:Gem::Dependency
|
31
47
|
name: rspec
|
32
48
|
requirement: !ruby/object:Gem::Requirement
|
@@ -59,6 +75,38 @@ dependencies:
|
|
59
75
|
- - ! '>='
|
60
76
|
- !ruby/object:Gem::Version
|
61
77
|
version: '0'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: active_record_no_table
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
- !ruby/object:Gem::Dependency
|
95
|
+
name: mysql2
|
96
|
+
requirement: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ! '>='
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
type: :development
|
103
|
+
prerelease: false
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ! '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
62
110
|
description: Extend core methods in Rails
|
63
111
|
email:
|
64
112
|
- mirrec@gmail.com
|
@@ -73,7 +121,13 @@ files:
|
|
73
121
|
- README.md
|
74
122
|
- Rakefile
|
75
123
|
- changelog.md
|
124
|
+
- db/schema.rb
|
76
125
|
- lib/wnm_support.rb
|
126
|
+
- lib/wnm_support/active_record_ext/destroy_validation.rb
|
127
|
+
- lib/wnm_support/active_record_ext/multi_action.rb
|
128
|
+
- lib/wnm_support/active_record_ext/mysql_order_by_field.rb
|
129
|
+
- lib/wnm_support/active_record_ext/mysql_truncate.rb
|
130
|
+
- lib/wnm_support/active_record_ext/view_helpers.rb
|
77
131
|
- lib/wnm_support/core_ext/class_exists.rb
|
78
132
|
- lib/wnm_support/core_ext/halved.rb
|
79
133
|
- lib/wnm_support/core_ext/is_integer.rb
|
@@ -85,6 +139,11 @@ files:
|
|
85
139
|
- lib/wnm_support/core_ext/unicode.rb
|
86
140
|
- lib/wnm_support/version.rb
|
87
141
|
- spec/spec_helper.rb
|
142
|
+
- spec/wnm_support/activer_record_ext/destroy_validation_spec.rb
|
143
|
+
- spec/wnm_support/activer_record_ext/multi_action_spec.rb
|
144
|
+
- spec/wnm_support/activer_record_ext/mysql_order_by_filed_spec.rb
|
145
|
+
- spec/wnm_support/activer_record_ext/mysql_truncate_spec.rb
|
146
|
+
- spec/wnm_support/activer_record_ext/view_helpers_spec.rb
|
88
147
|
- spec/wnm_support/core_ext/array_spec.rb
|
89
148
|
- spec/wnm_support/core_ext/class_exists_spec.rb
|
90
149
|
- spec/wnm_support/core_ext/false_class_spec.rb
|
@@ -119,6 +178,11 @@ specification_version: 3
|
|
119
178
|
summary: Extend core method in Rails
|
120
179
|
test_files:
|
121
180
|
- spec/spec_helper.rb
|
181
|
+
- spec/wnm_support/activer_record_ext/destroy_validation_spec.rb
|
182
|
+
- spec/wnm_support/activer_record_ext/multi_action_spec.rb
|
183
|
+
- spec/wnm_support/activer_record_ext/mysql_order_by_filed_spec.rb
|
184
|
+
- spec/wnm_support/activer_record_ext/mysql_truncate_spec.rb
|
185
|
+
- spec/wnm_support/activer_record_ext/view_helpers_spec.rb
|
122
186
|
- spec/wnm_support/core_ext/array_spec.rb
|
123
187
|
- spec/wnm_support/core_ext/class_exists_spec.rb
|
124
188
|
- spec/wnm_support/core_ext/false_class_spec.rb
|