blue_pages 0.1.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,7 +5,7 @@ class BluePages::PagesController < ::ApplicationController
5
5
  respond_to :html
6
6
 
7
7
  def show
8
- @page = BluePages::Page.where(:path => params[:path]).first
8
+ @page = ::Page.where(:path => params[:path]).first
9
9
  if @page
10
10
  render_page
11
11
  else
@@ -16,9 +16,21 @@ class BluePages::PagesController < ::ApplicationController
16
16
  protected
17
17
 
18
18
  def render_page
19
+ if BluePages.layout.present?
20
+ render_text
21
+ else
22
+ render_app_page
23
+ end
24
+ end
25
+
26
+ def render_app_page
19
27
  render 'pages/show'
20
28
  end
21
29
 
30
+ def render_text
31
+ render :text => @page.to_html, :layout => BluePages.layout
32
+ end
33
+
22
34
  def render_404
23
35
  render :file => File.join(Rails.root, "/public/404.html"),
24
36
  :status => :not_found,
@@ -0,0 +1,87 @@
1
+ # encoding: utf-8
2
+
3
+ require 'mongoid/tree'
4
+ require 'bluecloth'
5
+ require 'redcloth'
6
+
7
+ class Page
8
+
9
+ include Mongoid::Document
10
+ include Mongoid::Timestamps
11
+ include Mongoid::Tree
12
+ include LocalizedKeys
13
+
14
+ (BluePages.model_includes || []).each do |mod|
15
+ include mod
16
+ end
17
+
18
+ store_in :blue_pages
19
+
20
+ i18n_field :title
21
+ i18n_field :content
22
+ field :permalink
23
+ field :path
24
+ field :meta_data, :type => Hash
25
+ field :published, :type => Boolean, :default => true
26
+ field :custom_permalink, :type => Boolean, :default => false
27
+ field :filter, :default => 'none'
28
+
29
+ validates_presence_of :title
30
+ validates_uniqueness_of :title
31
+ validates_inclusion_of :filter, :in => %w( none markdown )
32
+
33
+ scope :published, where(:published => true)
34
+ scope :unpublished, where(:published => false)
35
+
36
+ before_save :check_permalink_presence
37
+ after_rearrange :rebuild_path
38
+
39
+ # Sets title and generates permalink if custom permalink is disabled.
40
+ def title=(new_title)
41
+ write_attribute(:permalink, Permalink.from(new_title)) unless self.custom_permalink?
42
+ write_attribute(:title, new_title)
43
+ end
44
+
45
+ # Sets permalink only if custom permalink is enabled.
46
+ def permalink=(permalink)
47
+ write_attribute(:permalink, permalink) if self.custom_permalink?
48
+ end
49
+
50
+ def to_s
51
+ title
52
+ end
53
+
54
+ def preferred_content
55
+ lang = I18n.locale.to_s
56
+ if content(lang).present?
57
+ content(lang)
58
+ else
59
+ content
60
+ end
61
+ end
62
+
63
+ def to_html
64
+ content = I18n.locale
65
+ filters = { 'markdown' => BlueCloth,
66
+ 'textile' => RedCloth }
67
+ if filters.include?(self.filter)
68
+ filters[self.filter].new(self.preferred_content).to_html
69
+ else
70
+ self.preferred_content
71
+ end
72
+ end
73
+
74
+ protected
75
+
76
+ def check_permalink_presence
77
+ unless self.permalink.present? || self.custom_permalink?
78
+ write_attribute(:permalink, Permalink.from(title)) if title
79
+ end
80
+ end
81
+
82
+ def rebuild_path
83
+ check_permalink_presence
84
+ self.path = self.ancestors_and_self.collect(&:permalink).join('/')
85
+ end
86
+
87
+ end
@@ -1,6 +1,6 @@
1
1
  # encoding: utf-8
2
2
 
3
- Rails::Application.routes.draw do
3
+ Rails.application.routes.draw do
4
4
 
5
5
 
6
6
  if BluePages.route_prefix.present?
@@ -1,5 +1,8 @@
1
1
  # encoding: utf-8
2
2
 
3
+ require 'permalink'
4
+ require 'blue_pages/localized_keys'
5
+
3
6
  module BluePages
4
7
 
5
8
  class Engine < Rails::Engine
@@ -10,7 +13,7 @@ module BluePages
10
13
  end
11
14
 
12
15
  mattr_accessor :route_prefix
13
-
16
+ mattr_accessor :layout
14
17
  mattr_accessor :model_includes
15
18
 
16
19
  end
@@ -0,0 +1,119 @@
1
+ # encoding: utf-8
2
+
3
+ module LocalizedKeys
4
+
5
+ extend ActiveSupport::Concern
6
+
7
+ included do |base|
8
+
9
+ before_validation :normalize_language
10
+
11
+ end
12
+
13
+
14
+ module ClassMethods
15
+
16
+ def localized_keys
17
+ @localized_keys ||= []
18
+ end
19
+
20
+ def default_language(instance = nil)
21
+ if @default_language.respond_to?(:call)
22
+ @default_language.call
23
+ elsif @default_language.is_a?(Symbol)
24
+ instance && instance.send(@default_language)
25
+ else
26
+ @default_language
27
+ end
28
+ end
29
+
30
+ def default_language=(language)
31
+ @default_language = language
32
+ end
33
+
34
+ def i18n_field(name, options = {})
35
+ localized_keys << name
36
+ self.default_language = options.delete(:default_language) if options[:default_language]
37
+
38
+ # Define MongoMapper key
39
+ key_name = "#{name}_locales"
40
+ self.field key_name, options.merge(:type => Hash, :default => {})
41
+
42
+ # Define a getter method
43
+ define_method(name) do |*args|
44
+ locale = (args.empty? || args.first.nil?) ?
45
+ self.class.default_language(self) :
46
+ args.first
47
+ locale_attr = read_attribute(key_name)
48
+ locale = locale_attr.keys.first unless locale
49
+ p locale
50
+ locale_attr[locale || ''] if locale_attr
51
+ end
52
+
53
+ # Define a setter method
54
+ define_method(:"#{name}=") do |*args|
55
+ locale_attr = read_attribute(key_name)
56
+ write_attribute(key_name, {}) unless locale_attr
57
+ read_attribute(key_name)[self.class.default_language(self) || ''] = args.first
58
+ end
59
+
60
+ # Define a query method
61
+ define_method(:"#{name}?") do |*args|
62
+ self.send(name, *args).present?
63
+ end
64
+ end
65
+
66
+ end
67
+
68
+
69
+ module InstanceMethods
70
+
71
+ def method_missing(name, *args)
72
+ if self.respond_to_dynamic?(name)
73
+ name.to_s =~ /(.*)_([a-z]{2})(=)??$/
74
+ key, locale, assignment = $1, $2, $3
75
+ localized_key = :"#{key}_locales"
76
+
77
+ if assignment.nil?
78
+ value = send(localized_key)
79
+ value[locale] if value
80
+ elsif
81
+ value = send(localized_key)
82
+ send(:"#{localized_key}=", {}) unless value
83
+ send(localized_key)[locale] = args.first
84
+ end
85
+ else
86
+ super
87
+ end
88
+ end
89
+
90
+ def respond_to?(*args)
91
+ super || self.respond_to_dynamic?(args.first)
92
+ end
93
+
94
+ protected
95
+
96
+ def respond_to_dynamic?(name)
97
+ if name.to_s =~ /(.*)_([a-z]{2})=?$/
98
+ key = $1
99
+ self.class.localized_keys.include?(key.to_sym)
100
+ else
101
+ false
102
+ end
103
+ end
104
+
105
+ def normalize_language
106
+ self.class.localized_keys.each do |key|
107
+ locales = self.send(:"#{key}_locales")
108
+
109
+ if locales && locales.has_key?("") && default = self.class.default_language(self)
110
+ locales[default] = locales[""]
111
+ locales.delete("")
112
+ end
113
+ end
114
+ end
115
+
116
+ end
117
+
118
+ end
119
+
@@ -0,0 +1,20 @@
1
+ require 'unicode_utils' if "".encoding_aware?
2
+
3
+ module Permalink
4
+
5
+ def self.from(string)
6
+ # Remove chars with accents
7
+ string = if "".encoding_aware?
8
+ UnicodeUtils.nfkd(string).gsub(/[^\x00-\x7F]/,'').downcase.to_s
9
+ else
10
+ string.mb_chars.normalize(:kd).gsub(/[^\x00-\x7F]/n,'').downcase.to_s
11
+ end
12
+
13
+ # Remove any non alphanumeric chars
14
+ string.gsub("&amp;", "and").
15
+ gsub(/[^a-z0-9\-_]/i, '-').
16
+ gsub(/-{2,}/, '-').
17
+ gsub(/(^-+|-+$)/, '')
18
+ end
19
+
20
+ end
metadata CHANGED
@@ -1,97 +1,96 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: blue_pages
3
- version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 1
8
- - 0
9
- version: 0.1.0
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.1
5
+ prerelease:
10
6
  platform: ruby
11
- authors:
12
- - "Ji\xC5\x99\xC3\xAD Zajpt"
7
+ authors:
8
+ - Jiri Zajpt
13
9
  autorequire:
14
10
  bindir: bin
15
11
  cert_chain: []
16
-
17
- date: 2010-11-14 00:00:00 +01:00
18
- default_executable:
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
12
+ date: 2011-11-09 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
21
15
  name: mongoid
22
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: &70141271321420 !ruby/object:Gem::Requirement
23
17
  none: false
24
- requirements:
18
+ requirements:
25
19
  - - ~>
26
- - !ruby/object:Gem::Version
27
- segments:
28
- - 2
29
- - 0
30
- - 0
31
- - beta
32
- - 20
33
- version: 2.0.0.beta.20
20
+ - !ruby/object:Gem::Version
21
+ version: '2.0'
34
22
  type: :runtime
35
23
  prerelease: false
36
- version_requirements: *id001
37
- - !ruby/object:Gem::Dependency
24
+ version_requirements: *70141271321420
25
+ - !ruby/object:Gem::Dependency
38
26
  name: mongoid-tree
39
- requirement: &id002 !ruby/object:Gem::Requirement
27
+ requirement: &70141271320620 !ruby/object:Gem::Requirement
40
28
  none: false
41
- requirements:
42
- - - ">="
43
- - !ruby/object:Gem::Version
44
- segments:
45
- - 0
46
- version: "0"
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70141271320620
36
+ - !ruby/object:Gem::Dependency
37
+ name: bluecloth
38
+ requirement: &70141271320100 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: 2.0.9
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *70141271320100
47
+ - !ruby/object:Gem::Dependency
48
+ name: RedCloth
49
+ requirement: &70141271319520 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 4.2.3
47
55
  type: :runtime
48
56
  prerelease: false
49
- version_requirements: *id002
57
+ version_requirements: *70141271319520
50
58
  description: Pages engine handles cms-like pages.
51
59
  email: jzajpt@blueberry.cz
52
60
  executables: []
53
-
54
61
  extensions: []
55
-
56
- extra_rdoc_files:
62
+ extra_rdoc_files:
57
63
  - README
58
- files:
64
+ files:
59
65
  - app/controllers/blue_pages/pages_controller.rb
60
- - app/models/blue_pages/page.rb
66
+ - app/models/page.rb
61
67
  - config/routes.rb
62
68
  - lib/blue_pages.rb
69
+ - lib/blue_pages/localized_keys.rb
70
+ - lib/permalink.rb
63
71
  - README
64
- has_rdoc: true
65
72
  homepage: http://github.com/jzajpt/blue_pages
66
73
  licenses: []
67
-
68
74
  post_install_message:
69
75
  rdoc_options: []
70
-
71
- require_paths:
76
+ require_paths:
72
77
  - lib
73
- required_ruby_version: !ruby/object:Gem::Requirement
78
+ required_ruby_version: !ruby/object:Gem::Requirement
74
79
  none: false
75
- requirements:
76
- - - ">="
77
- - !ruby/object:Gem::Version
78
- segments:
79
- - 0
80
- version: "0"
81
- required_rubygems_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ! '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
85
  none: false
83
- requirements:
84
- - - ">="
85
- - !ruby/object:Gem::Version
86
- segments:
87
- - 0
88
- version: "0"
86
+ requirements:
87
+ - - ! '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
89
90
  requirements: []
90
-
91
91
  rubyforge_project:
92
- rubygems_version: 1.3.7
92
+ rubygems_version: 1.8.10
93
93
  signing_key:
94
94
  specification_version: 3
95
95
  summary: Pages Engine for Rails 3
96
96
  test_files: []
97
-
@@ -1,45 +0,0 @@
1
- # encoding: utf-8
2
-
3
- class BluePages::Page
4
-
5
- include Mongoid::Document
6
- include Mongoid::Timestamps
7
- include Mongoid::Tree
8
-
9
- BluePages.model_includes.each do |mod|
10
- include mod
11
- end
12
-
13
- store_in :blue_pages
14
-
15
- field :title
16
- field :permalink
17
- field :path
18
- field :metadata, :type => Hash
19
- field :content
20
- field :published, :type => Boolean, :default => true
21
-
22
- validates_presence_of :title
23
- validates_uniqueness_of :title
24
-
25
- scope :published, where(:published => true)
26
- scope :unpublished, where(:published => false)
27
-
28
- after_rearrange :rebuild_path
29
-
30
- def title=(new_title)
31
- write_attribute(:permalink, Permalink.from(new_title))
32
- write_attribute(:title, new_title)
33
- end
34
-
35
- def to_s
36
- title
37
- end
38
-
39
- protected
40
-
41
- def rebuild_path
42
- self.path = self.ancestors_and_self.collect(&:permalink).join('/')
43
- end
44
-
45
- end