acts_as_page 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
data/lib/acts_as_page.rb
CHANGED
@@ -1,17 +1,21 @@
|
|
1
1
|
require 'globalize3'
|
2
2
|
require 'friendly_id'
|
3
3
|
require 'models/page_part'
|
4
|
+
require 'acts_as_page/part_composition'
|
5
|
+
require 'acts_as_page/home_page'
|
6
|
+
require 'acts_as_page/search_with_globalize'
|
4
7
|
|
5
8
|
module ActsAsPage
|
6
9
|
def self.included(base)
|
7
10
|
base.extend ClassMethods
|
11
|
+
base.extend SearchWithGlobalize
|
12
|
+
base.extend HomePage
|
8
13
|
end
|
9
14
|
|
10
15
|
module ClassMethods
|
11
16
|
def acts_as_page
|
12
17
|
extend FriendlyId
|
13
|
-
|
14
|
-
|
18
|
+
include PartComposition
|
15
19
|
validates :title, presence: true, uniqueness: true
|
16
20
|
|
17
21
|
attr_accessible :title, :home, :meta_keywords, :meta_description
|
@@ -19,32 +23,6 @@ module ActsAsPage
|
|
19
23
|
|
20
24
|
friendly_id :title, use: [:slugged, :globalize]
|
21
25
|
end
|
22
|
-
|
23
|
-
def set_home_page(model)
|
24
|
-
model.class.update_all(home: false)
|
25
|
-
model.update_attribute(:home, true)
|
26
|
-
end
|
27
|
-
|
28
|
-
#solution take from https://github.com/refinery/refinerycms/blob/2-0-stable/pages/app/models/refinery/page.rb#L149-L159
|
29
|
-
def with_globalize(conditions = {})
|
30
|
-
conditions = {locale: ::Globalize.locale.to_s}.merge(conditions)
|
31
|
-
globalized_conditions = {}
|
32
|
-
conditions.keys.each do |key|
|
33
|
-
if (translated_attribute_names.map(&:to_s) | %w(locale)).include?(key.to_s)
|
34
|
-
globalized_conditions["#{self.translation_class.table_name}.#{key}"] = conditions.delete(key)
|
35
|
-
end
|
36
|
-
end
|
37
|
-
joins(:translations).where(globalized_conditions).where(conditions).readonly(false)
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
def method_missing(name)
|
42
|
-
part = parts.with_globalize(title: name.to_s.downcase.underscore).first
|
43
|
-
if part
|
44
|
-
return part.body
|
45
|
-
else
|
46
|
-
super(name)
|
47
|
-
end
|
48
26
|
end
|
49
27
|
end
|
50
28
|
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module PartComposition
|
2
|
+
def self.included(model_class)
|
3
|
+
model_class.class_eval do
|
4
|
+
has_many :parts, class_name: 'PagePart'
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
def method_missing(name, *args)
|
9
|
+
title = name.to_s.downcase.underscore
|
10
|
+
if title.end_with?('=')
|
11
|
+
return update_part(title, args.first)
|
12
|
+
else
|
13
|
+
part = find_part(title)
|
14
|
+
if part
|
15
|
+
return part.body
|
16
|
+
else
|
17
|
+
super(name, *args)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
def update_part(title, attribute)
|
24
|
+
part = find_part(title.chop)
|
25
|
+
return part.update_attribute(:body, attribute)
|
26
|
+
end
|
27
|
+
|
28
|
+
def find_part(title)
|
29
|
+
parts.with_globalize(title: title).first
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module SearchWithGlobalize
|
2
|
+
#solution take from https://github.com/refinery/refinerycms/blob/2-0-stable/pages/app/models/refinery/page.rb#L149-L159
|
3
|
+
def with_globalize(conditions = {})
|
4
|
+
conditions = {locale: ::Globalize.locale.to_s}.merge(conditions)
|
5
|
+
globalized_conditions = {}
|
6
|
+
conditions.keys.each do |key|
|
7
|
+
if (translated_attribute_names.map(&:to_s) | %w(locale)).include?(key.to_s)
|
8
|
+
globalized_conditions["#{self.translation_class.table_name}.#{key}"] = conditions.delete(key)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
joins(:translations).where(globalized_conditions).where(conditions).readonly(false)
|
12
|
+
end
|
13
|
+
end
|
data/lib/acts_as_page/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: acts_as_page
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-04-
|
12
|
+
date: 2013-04-13 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
@@ -115,7 +115,10 @@ extensions: []
|
|
115
115
|
extra_rdoc_files: []
|
116
116
|
files:
|
117
117
|
- lib/tasks/acts_as_page_tasks.rake
|
118
|
+
- lib/acts_as_page/search_with_globalize.rb
|
119
|
+
- lib/acts_as_page/part_composition.rb
|
118
120
|
- lib/acts_as_page/version.rb
|
121
|
+
- lib/acts_as_page/home_page.rb
|
119
122
|
- lib/acts_as_page.rb
|
120
123
|
- lib/models/page_part.rb
|
121
124
|
- MIT-LICENSE
|