active_content 0.1.1 → 0.1.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 95694815bebd13b3b30d8b1d065d6b04dce20877
4
- data.tar.gz: 6cc2f2055d69201c469dafd1c8cac6e989b57930
3
+ metadata.gz: d60c817352bd84ece447640c005b19fa387d2949
4
+ data.tar.gz: cd81840e8c606e71f216a3aef698635f78f55bc6
5
5
  SHA512:
6
- metadata.gz: eb88e592627b511a9a56641cb1ff48e7a88fbb22ba3446fc7fe588c92850ee3d8379163b92aada99a2afb5e905d05934befb04b209679ec550f33537cfba09f8
7
- data.tar.gz: 57618528440456b8cfaf98a388115dc1d425b247185fb318670f4c97a7bbc7f91152bec1fb72abf169c0e123f7c74fcdde4cd709ef020e616ffc70e2c9ddf516
6
+ metadata.gz: a7f591a16993b72bb4c9a1f404bd08363f61c7c482db47861b7c663535bcb8aa5e64d8ac5d967f1d2b4e0bc154ace717788a1856e5079ff3459e380edf3f2a00
7
+ data.tar.gz: 23d890828c76d054afa886cba0d61bb0c505b38e076e31533957d28590413f8136cf4d1ad7a9c98f0557dbbb4fccea63ca84a199426f9b259dddb09631eb28ac
@@ -8,14 +8,14 @@ module ActiveContent
8
8
 
9
9
  assoc_opts = { as: :metable, class_name: 'ActiveContent::Metum', autosave: true, dependent: :destroy }
10
10
  assoc_proc = -> { where field: field }
11
- value_opts = { to: :"#{field}_metum", prefix: field, alias: field, default: options[:default] }
11
+ value_opts = options.merge(to: :"#{field}_metum", prefix: field, alias: field)
12
12
 
13
13
  has_one :"#{field}_metum", assoc_proc, assoc_opts
14
14
  delegate_attribute :value, cast_type, value_opts
15
15
 
16
16
  before_save do
17
17
  current = send(field)
18
- default = self.class.column_defaults["#{field}"]
18
+ default = self.class.try(:"_attribute_#{field}_value_default")
19
19
 
20
20
  if current.blank? or current == default
21
21
  send :"#{field}_metum=", nil
@@ -7,4 +7,11 @@ class ActiveContent::Profile < ApplicationRecord
7
7
 
8
8
  # Mount carrierwave uploader
9
9
  mount_uploader :image
10
+
11
+ # Name composed by first and last name
12
+ def name
13
+ if first_name or last_name
14
+ "#{first_name} #{last_name}".strip
15
+ end
16
+ end
10
17
  end
@@ -4,4 +4,7 @@ class ActiveContent::Taxonomy < ApplicationRecord
4
4
 
5
5
  # Define validations
6
6
  validates :name, presence: true
7
+
8
+ # Has associations
9
+ has_many :taxonomizations
7
10
  end
@@ -20,12 +20,6 @@ module ActiveContent
20
20
  end
21
21
  end
22
22
 
23
- define_method :name do
24
- if first_name or last_name
25
- "#{first_name} #{last_name}".strip
26
- end
27
- end
28
-
29
23
  define_method :gravatar do |size=48, default='mm'|
30
24
  mail = try(:email)
31
25
  hash = Digest::MD5::hexdigest(mail) unless mail.nil?
@@ -8,16 +8,17 @@ module ActiveContent
8
8
  assoc_opts = { as: :relatable, class_name: 'ActiveContent::Relation', autosave: true, dependent: :destroy }
9
9
  assoc_proc = -> { where field: field }
10
10
 
11
+ default = options.delete(:default)
11
12
  multiple = options.delete(:multiple)
12
- defaults = options.reverse_merge(class_name: "#{name}".classify, source: :item, source_type: assoc_type)
13
+ options = options.reverse_merge(class_name: "#{name}".classify, source: :item, source_type: assoc_type)
13
14
 
14
15
  if multiple
15
- options = defaults.merge(through: :"#{field}_relations")
16
+ options = options.merge(through: :"#{field}_relations")
16
17
 
17
18
  has_many options[:through], assoc_proc, assoc_opts
18
19
  has_many :"#{field}", options
19
20
  else
20
- options = defaults.merge(through: :"#{field}_relation")
21
+ options = options.merge(through: :"#{field}_relation")
21
22
 
22
23
  has_one options[:through], assoc_proc, assoc_opts
23
24
  has_one :"#{field}", options
@@ -30,6 +31,21 @@ module ActiveContent
30
31
  send :"#{field}=", "#{name}".classify.constantize.find_by_id(value)
31
32
  end
32
33
  end
34
+
35
+ if default
36
+ relation_method = :"_relation_#{field}_default"
37
+ relation_default = default
38
+
39
+ define_singleton_method relation_method do
40
+ relation_default
41
+ end
42
+
43
+ after_initialize do |record|
44
+ if record.try(:"#{field}").blank?
45
+ record.send(:"#{field}=", self.class.try(relation_method))
46
+ end
47
+ end
48
+ end
33
49
  end
34
50
  end
35
51
  end
@@ -4,7 +4,7 @@ module ActiveContent
4
4
 
5
5
  class_methods do
6
6
  def has_taxonomy(name, options={})
7
- options = options.reverse_merge(class_name: name.to_s.classify, source: :taxonomy)
7
+ options = options.reverse_merge(class_name: "#{name}".classify, source: :taxonomy)
8
8
  options = options.merge(through: :"#{name}_taxonomizations")
9
9
  setting = { as: :taxonomizable, class_name: 'ActiveContent::Taxonomization', autosave: true, dependent: :destroy }
10
10
 
@@ -37,6 +37,13 @@ module ActiveContent
37
37
  ids.empty? ? query.where(blank) : query.where(blank).or(query.where.not(id))
38
38
  end
39
39
  end
40
+
41
+ def has_taxonomized(name, options={})
42
+ assoc_name = "#{name}".pluralize
43
+ assoc_type = (options[:class_name] || "#{name}".classify).constantize.base_class.name
44
+
45
+ has_many :"#{assoc_name}", through: :taxonomizations, source: :taxonomizable, source_type: "#{assoc_type}"
46
+ end
40
47
  end
41
48
  end
42
49
  end
@@ -10,24 +10,11 @@ module ActiveContent
10
10
  enumerize :template, in: templates, predicates: { prefix: true }
11
11
 
12
12
  has_one :view_template, as: :templatable, class_name: 'ActiveContent::Template', autosave: true, dependent: :destroy
13
- delegate_attribute :name, :string, to: :view_template, prefix: 'template', alias: :template
13
+ delegate_attribute :name, :string, to: :view_template, prefix: 'template', alias: :template, finder: true, scope: true
14
14
 
15
15
  before_save do
16
16
  self.view_template = nil if template.blank?
17
17
  end
18
-
19
- query = self.includes(:view_template)
20
- blank = { templates: { name: nil } }
21
-
22
- scope :with_template, -> (*names) do
23
- named = { templates: { name: names } }
24
- names.empty? ? query.where.not(blank) : query.where(named)
25
- end
26
-
27
- scope :without_template, -> (*names) do
28
- named = { templates: { name: names } }
29
- names.empty? ? query.where(blank) : query.where(blank).or(query.where.not(named))
30
- end
31
18
  end
32
19
  end
33
20
  end
@@ -1,3 +1,3 @@
1
1
  module ActiveContent
2
- VERSION = '0.1.1'
2
+ VERSION = '0.1.2'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_content
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Olibia Tsati
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2017-11-13 00:00:00.000000000 Z
12
+ date: 2017-11-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord