bsm_models 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -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")].sort.each do |file|
18
+ file.sub(matcher, '\1').camelize.constantize
19
+ end
20
+ @__eagerly_constantized = true
21
+ end
22
+
23
+ end
24
+ end
@@ -0,0 +1,37 @@
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, options = {}, &block)
17
+ kind = (attributes = attributes.with_indifferent_access).delete(:kind) if attributes.is_a?(Hash)
18
+ klass = real_descendants.find {|k| k.kind == kind }
19
+ return super unless klass
20
+
21
+ klass.with_scope(current_scope) do
22
+ klass.new(attributes, options, &block)
23
+ end
24
+ end
25
+
26
+ def real_descendants
27
+ descendants.select(&:real_type?)
28
+ end
29
+
30
+ def kind
31
+ name.demodulize.underscore
32
+ end
33
+
34
+ end
35
+
36
+ delegate :kind, :to => 'self.class'
37
+ end
data/lib/bsm_models.rb CHANGED
@@ -5,6 +5,8 @@ module Bsm
5
5
  autoload :Abstract, 'bsm/model/abstract'
6
6
  autoload :Editable, 'bsm/model/editable'
7
7
  autoload :Deletable, 'bsm/model/deletable'
8
+ autoload :EagerDescendants, 'bsm/model/eager_descendants'
9
+ autoload :StiConvertable, 'bsm/model/sti_convertable'
8
10
  end
9
11
 
10
12
  class Railtie < ::Rails::Railtie
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bsm_models
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 1
8
+ - 2
9
9
  - 0
10
- version: 0.1.0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Dimitrij Denissenko
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-08-31 00:00:00 Z
18
+ date: 2011-09-01 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: abstract
@@ -59,6 +59,8 @@ files:
59
59
  - lib/bsm/model/abstract.rb
60
60
  - lib/bsm/model/locale/en.yml
61
61
  - lib/bsm/model/deletable.rb
62
+ - lib/bsm/model/eager_descendants.rb
63
+ - lib/bsm/model/sti_convertable.rb
62
64
  - lib/bsm/model/editable.rb
63
65
  - lib/bsm_models.rb
64
66
  homepage: https://github.com/bsm/models