spec/support/models.rb in amoeba-3.0.0 vs spec/support/models.rb in amoeba-3.1.0
- old
+ new
@@ -68,35 +68,50 @@
end
end
end
])
end
+
+ def truthy?
+ true
+ end
+
+ def falsey?
+ false
+ end
+
+ class << self
+ def tag_count
+ ActiveRecord::Base.connection.select_one('SELECT COUNT(*) AS tag_count FROM posts_tags')['tag_count']
+ end
+
+ def note_count
+ ActiveRecord::Base.connection.select_one('SELECT COUNT(*) AS note_count FROM notes_posts')['note_count']
+ end
+ end
end
class CustomThing < ActiveRecord::Base
belongs_to :post
+
class ArrayPack
def self.load(str)
- unless str.present? && str.length > 0
- return []
- end
- if str.is_a?(Array)
- return str
- end
+ return [] unless str.present?
+ return str if str.is_a?(Array)
str.split(',').map(&:to_i)
end
+
def self.dump(int_array)
- unless int_array.present? && int_array.length > 0
- return ''
- end
+ return '' unless int_array.present?
int_array.join(',')
end
end
serialize :value, ArrayPack
before_create :hydrate_me
+
def hydrate_me
self.value = value
end
end
@@ -183,14 +198,20 @@
# Inheritance
class Product < ActiveRecord::Base
has_many :images
has_and_belongs_to_many :sections
+ SECTION_COUNT_QUERY = 'SELECT COUNT(*) AS section_count FROM products_sections WHERE product_id = ?'.freeze
+
amoeba do
enable
propagate
end
+
+ def section_count
+ ActiveRecord::Base.connection.select_one(SECTION_COUNT_QUERY, id)['section_count']
+ end
end
class Section < ActiveRecord::Base
end
@@ -228,18 +249,55 @@
end
# Polymorphism
class Address < ActiveRecord::Base
belongs_to :addressable, polymorphic: true
+
+ amoeba do
+ enable
+ end
end
+class Photo < ActiveRecord::Base
+ belongs_to :imageable, polymorphic: true
+
+ amoeba do
+ customize(lambda { |original_photo,new_photo|
+ new_photo.name = original_photo.name.to_s + ' Copy'
+ })
+ end
+end
+
+class Company < ActiveRecord::Base
+ has_many :employees
+ has_many :customers
+
+ amoeba do
+ include_associations :employees,
+ :customers
+ end
+end
+
class Employee < ActiveRecord::Base
has_many :addresses, as: :addressable
+ has_many :photos, as: :imageable
+ belongs_to :company
+
+ amoeba do
+ include_associations [:addresses, :photos]
+ end
+
end
class Customer < ActiveRecord::Base
has_many :addresses, as: :addressable
+ has_many :photos, as: :imageable
+ belongs_to :company
+
+ amoeba do
+ enable
+ end
end
# Remapping and Method
class MetalObject < ActiveRecord::Base
@@ -256,21 +314,20 @@
def become_real
self.dup.becomes RealObject
end
- def remap_subobjects( relation_name )
+ def remap_subobjects(relation_name)
:subobjects if relation_name == :subobject_prototypes
end
end
class RealObject < MetalObject
has_many :subobjects, foreign_key: :parent_id
end
class SubobjectPrototype < MetalObject
-
amoeba do
enable
through :become_subobject
end
@@ -283,39 +340,34 @@
end
# Check of changing boolean attributes
class SuperAdmin < ::ActiveRecord::Base
-
amoeba do
set active: false
prepend password: false
end
end
# Proper inheritance
class Box < ActiveRecord::Base
-
has_many :products, class_name: 'BoxProduct'
has_many :sub_products, class_name: 'BoxSubProduct'
amoeba do
enable
end
-
end
class BoxProduct < ActiveRecord::Base
-
belongs_to :box, class_name: 'Box'
amoeba do
enable
propagate
end
-
end
class BoxSubProduct < BoxProduct
has_one :another_product, class_name: 'BoxAnotherProduct'
end
@@ -323,6 +375,41 @@
class BoxSubSubProduct < BoxSubProduct
end
class BoxAnotherProduct < BoxProduct
belongs_to :sub_product, class_name: 'BoxSubProduct'
-end
\ No newline at end of file
+end
+
+# Inclusion inheritance
+class Stage < ActiveRecord::Base
+ has_many :listeners
+ has_many :specialists
+
+ amoeba do
+ include_association :listeners
+ include_association :specialists
+ nullify :external_id
+ propagate
+ end
+end
+
+class Participant < ActiveRecord::Base
+ belongs_to :stage
+end
+
+class Listener < Participant
+end
+
+class Specialist < Participant
+end
+
+class CustomStage < Stage
+ has_many :custom_rules, foreign_key: :stage_id
+
+ amoeba do
+ include_association :custom_rules
+ end
+end
+
+class CustomRule < ActiveRecord::Base
+ belongs_to :custom_stage
+end