saasable 0.1.3 → 0.1.4

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.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.3
1
+ 0.1.4
@@ -8,6 +8,8 @@ module Saasable::SaasDocument
8
8
  @saas_document = klass
9
9
  end
10
10
 
11
+ klass.extend ClassMethods
12
+ klass.send(:include, InstanceMethods)
11
13
  klass.class_eval do
12
14
  field :hosts, :type => Array
13
15
  end
@@ -16,4 +18,38 @@ module Saasable::SaasDocument
16
18
  def self.saas_document
17
19
  @saas_document
18
20
  end
21
+
22
+ module InstanceMethods
23
+ def activate!
24
+ Saasable::ScopedDocument.scoped_documents.each do |klass|
25
+ # Create a default scope without messing with the ones already in place.
26
+ klass.default_scoping ||= {}
27
+ klass.default_scoping[:where] ||= {:saas_id => nil}
28
+
29
+ klass.default_scoping[:where][:saas_id] = self._id
30
+ klass.class_eval "field :saas_id, :type => BSON::ObjectId, :default => BSON::ObjectId(\"#{self._id}\")"
31
+ end
32
+ end
33
+ end
34
+
35
+ module ClassMethods
36
+ def find_by_host! a_host
37
+ if Saasable::SaasDocument.saas_document.nil?
38
+ if Rails.env.production?
39
+ raise Saasable::Errors::NoSaasDocuments, "you need to set one Saasable::SaasDocument"
40
+ else
41
+ return nil
42
+ end
43
+ end
44
+
45
+ possible_saas = Saasable::SaasDocument.saas_document.where(:hosts => a_host)
46
+ if possible_saas.empty?
47
+ raise Saasable::Errors::SaasNotFound, "no #{Saasable::SaasDocument.saas_document.name} found for the host: \"#{a_host}\""
48
+ elsif possible_saas.count > 1
49
+ raise Saasable::Errors::MultipleSaasFound, "more then 1 #{Saasable::SaasDocument.saas_document.name} found for the host: \"#{a_host}\""
50
+ else
51
+ return possible_saas.first
52
+ end
53
+ end
54
+ end
19
55
  end
@@ -16,35 +16,11 @@ module Saasable::ScopedController
16
16
 
17
17
  private
18
18
  def fetch_current_saas
19
- if Saasable::SaasDocument.saas_document.nil?
20
- if Rails.env.production?
21
- raise Saasable::Errors::NoSaasDocuments, "you need to set one Saasable::SaasDocument"
22
- else
23
- return @current_saas = nil
24
- end
25
- end
26
-
27
- possible_saas = Saasable::SaasDocument.saas_document.where(:hosts => request.host)
28
- if possible_saas.empty?
29
- raise Saasable::Errors::SaasNotFound, "no #{Saasable::SaasDocument.saas_document.name} found for the host: \"#{request.host}\""
30
- elsif possible_saas.count > 1
31
- raise Saasable::Errors::MultipleSaasFound, "more then 1 #{Saasable::SaasDocument.saas_document.name} found for the host: \"#{request.host}\""
32
- else
33
- @current_saas = possible_saas.first
34
- end
19
+ @current_saas = Saas::SaasDocument.find_by_host!(request.host)
35
20
  end
36
21
 
37
22
  def scope_models_by_saas
38
- Saasable::ScopedDocument.scoped_documents.each do |klass|
39
- # Create a default scope without messing with the ones already in place.
40
- klass.default_scoping ||= {}
41
- klass.default_scoping[:where] ||= {:saas_id => nil}
42
-
43
- if @current_saas
44
- klass.default_scoping[:where][:saas_id] = @current_saas._id
45
- klass.class_eval "field :saas_id, :type => BSON::ObjectId, :default => BSON::ObjectId(\"#{@current_saas._id}\")"
46
- end
47
- end
23
+ @current_saas.activate!
48
24
  end
49
25
  end
50
26
  end
@@ -4,6 +4,7 @@ module Saasable::ScopedDocument
4
4
  def self.included klass
5
5
  @scoped_documents << klass unless @scoped_documents.include? klass
6
6
 
7
+ klass.extend ClassMethods
7
8
  klass.class_eval do
8
9
  field :saas_id, :type => BSON::ObjectId
9
10
  end
@@ -12,4 +13,14 @@ module Saasable::ScopedDocument
12
13
  def self.scoped_documents
13
14
  @scoped_documents
14
15
  end
16
+
17
+ module ClassMethods
18
+ def validates_uniqueness_of(*args)
19
+ attributes = _merge_attributes(args)
20
+ attributes[:scope] ||= []
21
+ attributes[:scope] << :saas_id unless attributes[:scope].include?(:saas_id)
22
+
23
+ validates_with(Mongoid::Validations::UniquenessValidator, attributes)
24
+ end
25
+ end
15
26
  end
data/saasable.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{saasable}
8
- s.version = "0.1.3"
8
+ s.version = "0.1.4"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = [%q{Educa2}, %q{Rafael Gaspar}]
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: saasable
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 3
10
- version: 0.1.3
9
+ - 4
10
+ version: 0.1.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - Educa2