bsm-models 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/bsm-models.rb ADDED
@@ -0,0 +1 @@
1
+ require 'bsm/model'
data/lib/bsm/model.rb ADDED
@@ -0,0 +1,16 @@
1
+ require 'rails'
2
+
3
+ module Bsm
4
+ module Model
5
+ autoload :Abstract, 'bsm/model/abstract'
6
+ autoload :Editable, 'bsm/model/editable'
7
+ autoload :Deletable, 'bsm/model/deletable'
8
+ autoload :EagerDescendants, 'bsm/model/eager_descendants'
9
+ autoload :StiConvertable, 'bsm/model/sti_convertable'
10
+ end
11
+
12
+ class Railtie < ::Rails::Railtie
13
+ require 'active_support/i18n'
14
+ I18n.load_path << File.expand_path('../bsm/model/locale/en.yml', __FILE__)
15
+ end
16
+ end
@@ -0,0 +1,20 @@
1
+ module Bsm::Model::Abstract
2
+ extend ActiveSupport::Concern
3
+
4
+ included do
5
+ validate :must_not_be_abstract
6
+
7
+ define_method :must_not_be_abstract do
8
+ errors.add :base, :abstract if abstract_model_instance?
9
+ end
10
+
11
+ class_eval <<-END_EVAL
12
+ def abstract_model_instance?
13
+ self.class >= ::#{self.name}
14
+ end
15
+ END_EVAL
16
+
17
+ protected :must_not_be_abstract, :abstract_model_instance?
18
+ end
19
+
20
+ end
@@ -0,0 +1,12 @@
1
+ module Bsm::Model::Deletable
2
+ extend ActiveSupport::Concern
3
+
4
+ included do
5
+ before_destroy :deletable?
6
+ end
7
+
8
+ def deletable?
9
+ not_implemented
10
+ end
11
+
12
+ end
@@ -0,0 +1,24 @@
1
+ module Bsm::Model::EagerDescendants
2
+ extend ActiveSupport::Concern
3
+
4
+ module ClassMethods
5
+
6
+ def descendants
7
+ eager_constantize!
8
+ super
9
+ end
10
+
11
+ private
12
+
13
+ def eager_constantize!
14
+ return if @__eagerly_constantized
15
+ load_path = Rails.root.join("app", "models")
16
+ matcher = /\A#{Regexp.escape(load_path.to_s)}\/(.*)\.rb\Z/
17
+ Dir[load_path.join(self.parent_name.underscore, "**", "*.rb")].each do |file|
18
+ ActiveSupport::Dependencies.depend_on file
19
+ end
20
+ @__eagerly_constantized = true
21
+ end
22
+
23
+ end
24
+ end
@@ -0,0 +1,22 @@
1
+ module Bsm::Model::Editable
2
+ extend ActiveSupport::Concern
3
+
4
+ included do
5
+ validate :must_be_editable, :on => :update
6
+ end
7
+
8
+ def editable?
9
+ not_implemented
10
+ end
11
+
12
+ def immutable?
13
+ !editable?
14
+ end
15
+
16
+ protected
17
+
18
+ def must_be_editable
19
+ errors.add :base, :immutable if immutable?
20
+ end
21
+
22
+ end
@@ -0,0 +1,6 @@
1
+ en:
2
+ activerecord:
3
+ errors:
4
+ messages:
5
+ abstract: "Record is abstract"
6
+ immutable: "Record cannot be updated"
@@ -0,0 +1,43 @@
1
+ module Bsm::Model::StiConvertable
2
+ extend ActiveSupport::Concern
3
+
4
+ included do
5
+ include Bsm::Model::EagerDescendants
6
+ class_eval <<-METHOD, __FILE__, __LINE__ + 1
7
+ def self.real_type?
8
+ self < ::#{name}
9
+ end
10
+ METHOD
11
+ end
12
+
13
+ module ClassMethods
14
+
15
+ # @Override: Allow to specify a kind
16
+ def new(attributes = nil, *args, &block)
17
+ kind = (attributes = attributes.with_indifferent_access).delete(:kind) if attributes.is_a?(Hash)
18
+ return super if real_type?
19
+
20
+ klass = real_descendants.find {|k| k.kind == kind } || fallback_descendant
21
+ return super if klass == self
22
+
23
+ klass.with_scope(respond_to?(:current_scope) ? current_scope : current_scoped_methods) do # AR 3.0 compatibility
24
+ klass.new(attributes, *args, &block)
25
+ end
26
+ end
27
+
28
+ def real_descendants
29
+ descendants.select(&:real_type?)
30
+ end
31
+
32
+ def fallback_descendant
33
+ self
34
+ end
35
+
36
+ def kind
37
+ name.demodulize.underscore
38
+ end
39
+
40
+ end
41
+
42
+ delegate :kind, :to => 'self.class'
43
+ end
data/lib/bsm_models.rb ADDED
@@ -0,0 +1 @@
1
+ require 'bsm/model'
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bsm-models
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Dimitrij Denissenko
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-10-21 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: abstract
16
+ requirement: &23659180 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *23659180
25
+ - !ruby/object:Gem::Dependency
26
+ name: activerecord
27
+ requirement: &23658640 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 3.1.0
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *23658640
36
+ - !ruby/object:Gem::Dependency
37
+ name: railties
38
+ requirement: &23658080 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: 3.1.0
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *23658080
47
+ description: ''
48
+ email: dimitrij@blacksquaremedia.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - lib/bsm/model/abstract.rb
54
+ - lib/bsm/model/locale/en.yml
55
+ - lib/bsm/model/deletable.rb
56
+ - lib/bsm/model/eager_descendants.rb
57
+ - lib/bsm/model/sti_convertable.rb
58
+ - lib/bsm/model/editable.rb
59
+ - lib/bsm/model.rb
60
+ - lib/bsm_models.rb
61
+ - lib/bsm-models.rb
62
+ homepage: https://github.com/bsm/models
63
+ licenses: []
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ! '>='
72
+ - !ruby/object:Gem::Version
73
+ version: 1.8.7
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ! '>='
78
+ - !ruby/object:Gem::Version
79
+ version: 1.3.6
80
+ requirements: []
81
+ rubyforge_project:
82
+ rubygems_version: 1.8.10
83
+ signing_key:
84
+ specification_version: 3
85
+ summary: BSM's very custom model extensions
86
+ test_files: []