aerogel-core 1.3.0 → 1.4.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. checksums.yaml +4 -4
  2. data/aerogel-core.gemspec +8 -2
  3. data/app/helpers/assets.rb +22 -8
  4. data/app/helpers/core.rb +82 -0
  5. data/app/helpers/i18n.rb +11 -0
  6. data/app/helpers/icon.rb +12 -0
  7. data/app/helpers/render.rb +89 -17
  8. data/app/helpers/tags.rb +18 -3
  9. data/app/routes/aerogel.rb +30 -0
  10. data/app/routes/core.rb +16 -1
  11. data/app/routes/i18n.rb +14 -0
  12. data/config/application.conf +8 -0
  13. data/config/development/aerogel.conf +9 -0
  14. data/config/production/aerogel.conf +10 -0
  15. data/lib/aerogel/core/application.rb +3 -0
  16. data/lib/aerogel/core/assets.rb +24 -0
  17. data/lib/aerogel/core/cache.rb +72 -0
  18. data/lib/aerogel/core/config.rb +10 -2
  19. data/lib/aerogel/core/core.rb +55 -10
  20. data/lib/aerogel/core/core_ext/array.rb +38 -0
  21. data/lib/aerogel/core/core_ext/hash.rb +48 -0
  22. data/lib/aerogel/core/db.rb +28 -8
  23. data/lib/aerogel/core/db/model.rb +52 -4
  24. data/lib/aerogel/core/errors.rb +23 -1
  25. data/lib/aerogel/core/helpers.rb +1 -1
  26. data/lib/aerogel/core/i18n.rb +134 -0
  27. data/lib/aerogel/core/i18n/number_helper.rb +126 -0
  28. data/lib/aerogel/core/reloader.rb +49 -5
  29. data/lib/aerogel/core/routes.rb +5 -3
  30. data/lib/aerogel/core/routes/namespace.rb +1 -1
  31. data/lib/aerogel/core/version.rb +3 -1
  32. data/locales/core/en.yml +17 -0
  33. data/locales/core/ru.yml +17 -0
  34. data/locales/db/en.yml +25 -0
  35. data/locales/db/ru.yml +24 -0
  36. data/locales/i18n-defaults/en.yml +211 -0
  37. data/locales/i18n-defaults/ru.yml +257 -0
  38. data/rake/console.rake +8 -0
  39. data/views/layouts/application.html.erb +1 -1
  40. data/views/lorem.html.erb +48 -0
  41. metadata +91 -3
  42. data/app/routes/static.rb +0 -7
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b0b6c95d4a960593785baf5d48ffdf798bcaf5da
4
- data.tar.gz: 2c964b75f82fb8b0c141c93ca81ee3c9909a270f
3
+ metadata.gz: f2aa3cd909e476af3c30504c867493ffd5cee0f3
4
+ data.tar.gz: bd4abadb05d22159b7374d1ba705683745eac39c
5
5
  SHA512:
6
- metadata.gz: d544fb26f5f875bd4a27bffb261cadbd8638141d98188d47e15bd4ce3aaea77bc96eb2ca660087f9a71e266d425a2c13664c2d6361b7a833dfb177efab7d0de3
7
- data.tar.gz: 9dd900eb31268b5f3a523f1cb5326b6631dd2dd1aacc597404920cc172b9338e53dca948f867184f1ec975a1737a3a1e3986fa3974762a93a7cf865f65033c6b
6
+ metadata.gz: 77d0175b72a8c6e6b072708c2c9aebffdd812243e677e86cbff40618df166e862fad185642bc3c853249d461baaa55b869aecf736445d84180fd86b742783716
7
+ data.tar.gz: 8bd415aa95e4de394c1fb0b9087b3991764ee53968f0baa1de9402669743a164e8f77ea5f34f8e98928e83a9aaf5d935edbab5b37f638b17ba2a80bb38e4860e
data/aerogel-core.gemspec CHANGED
@@ -5,7 +5,7 @@ require 'aerogel/core/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "aerogel-core"
8
- spec.version = Aerogel::VERSION
8
+ spec.version = Aerogel::Core::VERSION
9
9
  spec.authors = ["Alex Kukushkin"]
10
10
  spec.email = ["alex@kukushk.in"]
11
11
  spec.description = %q{Aerogel core module}
@@ -22,14 +22,20 @@ Gem::Specification.new do |spec|
22
22
  spec.add_dependency 'sinatra-contrib'
23
23
  spec.add_dependency 'sinatra-asset-pipeline'
24
24
  spec.add_dependency 'rack-flash3'
25
+ spec.add_dependency 'sinatra-redirect-with-flash'
26
+ # spec.add_dependency 'active_support' #, "~> 4.0"
25
27
  spec.add_dependency 'sass'
26
28
  spec.add_dependency 'coffee-script'
27
29
  spec.add_dependency 'uglifier'
28
30
  spec.add_dependency 'yui-compressor'
29
31
  spec.add_dependency 'aerogel-configurator', "~> 1.3"
30
- spec.add_dependency 'mongoid'
32
+ spec.add_dependency 'mongoid', "~> 3.1"
33
+ spec.add_dependency 'mongoid-tree'
34
+ spec.add_dependency 'i18n'
35
+ spec.add_dependency 'lru_redux'
31
36
 
32
37
  spec.add_development_dependency "bundler", "~> 1.3"
33
38
  spec.add_development_dependency "rake"
39
+ spec.add_development_dependency "pry"
34
40
  end
35
41
 
@@ -1,11 +1,25 @@
1
- # Include styles and javascript tags for assets grouped by scope.
1
+ # Appends to and retrieves from declared assets stack.
2
2
  #
3
- # Intended use is either:
4
- # <%= assets %> # for application assets
5
- # or:
6
- # <% assets 'controller/name' %> # for controller specific assets
3
+ # To include application assets and retrieve assets tags:
4
+ # <%= assets %>
7
5
  #
8
- def assets( filename = :application )
9
- (stylesheet_tag filename.to_s) +
10
- (javascript_tag filename.to_s)
6
+ # To include/display controller's assets:
7
+ # <%= assets 'controller/name' %>
8
+ #
9
+ # To append to assets stack on a per-view basis:
10
+ # # in layout:
11
+ # <%= assets 'controller/name' %>
12
+ #
13
+ # # in view:
14
+ # <% assets 'controller/name/view' %> # note that helper output is ignored here, view asset tags
15
+ # # are rendered in layout
16
+ #
17
+ def assets( filename = nil )
18
+ @assets_stack ||= []
19
+ filename = :application if @assets_stack.blank? && filename.blank?
20
+ @assets_stack.unshift filename if filename.present?
21
+ @assets_stack.map do |assets_filename|
22
+ (stylesheet_tag assets_filename.to_s) +
23
+ (javascript_tag assets_filename.to_s)
24
+ end.join("\n")
11
25
  end
data/app/helpers/core.rb CHANGED
@@ -9,3 +9,85 @@ def logger( name = nil )
9
9
  env['rack.logger.'+name.to_s] or raise("Logger with name '#{name}' is not registered")
10
10
  end
11
11
  end
12
+
13
+ # Returns current_url
14
+ # To return current url in a different locale:
15
+ # current_url locale: :ru
16
+ #
17
+ def current_url( opts = {} )
18
+ url = URI.unescape( request.path_info )
19
+ opts.present? ? url_to( url, opts ) : url
20
+ end
21
+
22
+ # Returns current locale
23
+ #
24
+ def current_locale
25
+ I18n.locale
26
+ end
27
+
28
+ # Returns current hostname.
29
+ # If hostname is set in application configuration files, it has precedence.
30
+ # Otherwise, hostname is inferred from the +request.host+, stripped from potential
31
+ # locale name.
32
+ #
33
+ def current_hostname
34
+ return config.hostname! if config.hostname?
35
+ hostname_parts = request.host.split '.'
36
+ hostname_parts.shift if I18n.available_locales.include?( hostname_parts.first.to_sym )
37
+ hostname_parts.join "."
38
+ end
39
+
40
+ # Constructs an URL to a resource.
41
+ #
42
+ # Options passed in +opts+ will be appended to URL as a query string,
43
+ # except some reserved options which have special meaning:
44
+ # :locale => constructs URL with a certain locale
45
+ # :scheme => constructs URL with a certain protocol
46
+ # :fqdn => constructs full qualified URL, including hostname and protocol
47
+ #
48
+ # Example:
49
+ # url_to "/bar", locale: :de, page: 2, order: :name # => "http://de.example.org/bar?page=2&order=name"
50
+ #
51
+ def url_to( url, opts = {} )
52
+ hostname = nil
53
+ if opts[:fqdn] || opts[:scheme]
54
+ opts[:locale] ||= current_locale
55
+ end
56
+ if opts[:locale]
57
+ unless I18n.available_locales.include? opts[:locale]
58
+ raise ArgumentError.new("Unavailable locale '#{opts[:locale]}' passed to #url_to helper")
59
+ end
60
+ if opts[:locale] == I18n.default_locale
61
+ hostname = current_hostname
62
+ else
63
+ hostname = "#{opts[:locale]}.#{current_hostname}"
64
+ end
65
+ end
66
+ query_string = opts.except( :locale, :fqdn, :scheme ).map{|k,v| "#{k}=#{h v}"}.join "&"
67
+ if query_string.present?
68
+ if url =~ /\?/
69
+ query_string = "&#{query_string}"
70
+ else
71
+ query_string = "?#{query_string}"
72
+ end
73
+ end
74
+ scheme = opts[:scheme] || request.scheme || 'http'
75
+ scheme_hostname = hostname.nil? ? '' : "#{scheme}://#{hostname}"
76
+ "#{scheme_hostname}#{url}#{query_string}"
77
+ end
78
+
79
+ # xhr-conscious redirect.
80
+ #
81
+ def redirect(uri, *args)
82
+ if request.xhr?
83
+ if Hash === args.first
84
+ opts = args.first
85
+ [:error, :notice, :warning].each do |flash_key|
86
+ flash[flash_key] = opts[flash_key] if opts[flash_key].present?
87
+ end
88
+ end
89
+ halt 200, {'Content-Type' => 'text/javascript'}, "window.location.href=\"#{uri}\""
90
+ else
91
+ super( uri, *args )
92
+ end
93
+ end
@@ -0,0 +1,11 @@
1
+ # +t+ helper, alias for chainable helper Aerogel::I18n.t
2
+ #
3
+ def t( *args )
4
+ Aerogel::I18n.t( *args )
5
+ end
6
+
7
+ # +l+ helper, alias for Aerogel::I18n.l
8
+ #
9
+ def l( *args )
10
+ Aerogel::I18n.l( *args )
11
+ end
@@ -0,0 +1,12 @@
1
+ # Renders icon from icon font.
2
+ # First part of the +name+ defines icon family.
3
+ #
4
+ # Example:
5
+ # <%= icon 'fa-caret-right' %> # => icon 'caret-right' from Font Awesome (fa) family
6
+ # <%= icon 'glyphicon-tick' %> #=> icon 'tick' from Boostrap glyphicons
7
+ #
8
+ def icon( name, opts = {} )
9
+ icon_family = name.split('-').first
10
+ icon_class = "#{icon_family} #{name} #{opts[:class]}"
11
+ tag :i, "", opts.merge( class: icon_class )
12
+ end
@@ -1,24 +1,96 @@
1
- # module Aerogel::Helpers
2
1
 
3
- # Escapes html string.
4
- #
5
- def h( str )
6
- Rack::Utils.escape_html(str)
2
+ # Escapes html string.
3
+ #
4
+ def h( str )
5
+ Rack::Utils.escape_html(str)
6
+ end
7
+
8
+ # Displays text only first time the helper is called in this request
9
+ # Usage:
10
+ # render_once "hello, world"
11
+ # Or:
12
+ # render_once :hello_message, "hello, world"
13
+ #
14
+ def render_once( *args )
15
+ @render_once_hash ||= {}
16
+ if Symbol === args.first
17
+ key = args.shift
18
+ text = args.shift
19
+ else
20
+ key = args.shift
21
+ text = key
22
+ end
23
+ return '' if @render_once_hash.key? key
24
+ @render_once_hash[key] = true
25
+ text
26
+ end
27
+
28
+ # Renders erb template.
29
+ #
30
+ def view( name, opts = {} )
31
+ ts = Time.now
32
+ default_opts = {}
33
+ default_opts[:layout] = :"layouts/#{layout}.html" if layout.present?
34
+ #if settings.development?
35
+ erb( "#{name}.html".to_sym, default_opts.merge(opts) )+( "<!-- %s: %.3fs -->" % [name, Time.now - ts] )
36
+ #else
37
+ # erb( "#{name}.html".to_sym, default_opts.merge(opts) )
38
+ #end
39
+ end
40
+
41
+ # Renders partial erb template.
42
+ #
43
+ def partial( name, opts = {} )
44
+ if opts.key?( :cacheable ) && config.aerogel.cache.enabled?
45
+ Aerogel::Cache.cacheable current_locale, name, opts[:cacheable] do |key|
46
+ __uncached_partial( name, opts )+"<!-- cache #{key} @ #{Time.now} -->"
47
+ end
48
+ else
49
+ __uncached_partial( name, opts )
7
50
  end
51
+ end
52
+
53
+ def __uncached_partial( name, opts = {} )
54
+ name_parts = name.to_s.split('/')
55
+ partial_name = name_parts[-1]
56
+ name_parts[-1] = '_'+partial_name+".html"
57
+ template_name = name_parts.join('/').to_sym
58
+ opts[:layout] = false
8
59
 
9
- # Renders erb template.
10
- #
11
- def view( name, *args )
12
- erb( "#{name}.html".to_sym, *args )
60
+ # render single template
61
+ unless opts.key? :collection
62
+ return erb template_name, opts
13
63
  end
14
64
 
15
- # Renders partial erb template.
16
- #
17
- def partial( name, opts = {} )
18
- name_parts = name.to_s.split('/')
19
- name_parts[-1] = '_'+name_parts[-1]+".html"
20
- opts[:layout] = false
21
- erb name_parts.join('/').to_sym, opts
65
+ # render collection
66
+ out = ""
67
+ opts[:locals] ||= {}
68
+ opts[:collection].each do |object|
69
+ opts[:locals][partial_name.to_sym] = object
70
+ out += opts[:delimiter] if opts[:delimiter].present? && out.present?
71
+ out += erb template_name, opts.except( :collection, :delimiter, :cacheable )
22
72
  end
73
+ out
74
+ end
75
+
76
+ # Sets/gets page title.
77
+ #
78
+ # Example:
79
+ # page_title "Home page" # sets page title
80
+ # page_title # => "Home page"
81
+ #
82
+ # Or in views:
83
+ # <% page_title "Home page" %> # sets page title
84
+ # <%= page_title %> # gets page title
85
+ #
86
+ def page_title( value = nil )
87
+ @page_title = value unless value.nil?
88
+ @page_title
89
+ end
23
90
 
24
- # end # module Aerogel::Helpers
91
+ # Sets/gets current layout.
92
+ #
93
+ def layout( value = nil )
94
+ @layout = value unless value.nil?
95
+ @layout
96
+ end
data/app/helpers/tags.rb CHANGED
@@ -8,7 +8,7 @@ def tag( name, *args, &block )
8
8
  end
9
9
  attrs = args.shift || {}
10
10
  # t_attrs = attrs.map{|k,v| v.nil? ? " #{k}" : " #{k}=\"#{h(v)}\""}
11
- t_attrs = attrs.map{|k,v| v.nil? ? " #{k}" : " #{k}=\"#{(v)}\""}
11
+ t_attrs = attrs.map{|k,v| v.nil? ? " #{k}" : " #{k}=\"#{h(v)}\""}
12
12
  if content
13
13
  output = "<#{name}#{t_attrs.join}>"+content+"</#{name}>"
14
14
  else
@@ -24,6 +24,21 @@ end
24
24
 
25
25
  # Creates <a href=''..>...</a> tag.
26
26
  #
27
- def link_to( url, text = url, opts = {} )
28
- tag :a, text, opts.merge( href: url )
27
+ def link_to( url, *args, &block )
28
+ opts = (String === args.first) ? args[1] : args[0]
29
+ opts = {
30
+ href: url
31
+ }.deep_merge( opts || {} )
32
+ if String === args.first
33
+ args[1] = opts
34
+ else
35
+ args[0] = opts
36
+ end
37
+ tag :a, *args, &block
38
+ end
39
+
40
+ # Creates a <button ...>...</button> tag.
41
+ #
42
+ def button_to( url, text = url, opts = {} )
43
+ tag :button, text, opts.merge( url: url )
29
44
  end
@@ -0,0 +1,30 @@
1
+ # Diagnostic routes for aerogel application
2
+ #
3
+
4
+ namespace "/aerogel" do
5
+
6
+ # Displays aerogel status info
7
+ #
8
+ get "/status" do
9
+ ruby_version = "#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}"
10
+ modules_loaded = Aerogel.registered_paths.map{|p| p[:module_name]}.sort
11
+ [
12
+ "ruby:#{ruby_version}, aerogel-core v#{Aerogel::Core::VERSION}-#{settings.environment}",
13
+ "modules loaded: #{modules_loaded.join(", ")}",
14
+ "application path: #{Aerogel.application_path}",
15
+ "reloader: #{ config.aerogel.reloader? ? 'enabled' : 'disabled' }",
16
+ "cache: #{ config.aerogel.cache.enabled? ? 'enabled' : 'disabled' }"
17
+ ].join("<br/>\n")
18
+ end
19
+
20
+
21
+ # Displays cache info
22
+ #
23
+ get "/cache" do
24
+ [
25
+ ( config.aerogel.cache.enabled? ? "enabled" : "disabled" ),
26
+ "objects: #{Aerogel::Cache.keys.size} of #{config.aerogel.cache.max_size}"
27
+ ].join("<br/>\n")
28
+ end
29
+
30
+ end
data/app/routes/core.rb CHANGED
@@ -1,9 +1,24 @@
1
+ # serve static files
2
+ get "/*" do |filename|
3
+ pass if filename.blank?
4
+ path = Aerogel.get_resource( :public, filename )
5
+ pass unless path
6
+ pass unless File.file? path
7
+ send_file path
8
+ end
9
+
10
+ # serve default root
1
11
  get "/" do
2
12
  view :index
3
13
  end
4
14
 
15
+ # serve default root actions
5
16
  get "/:action" do
6
- view params['action'] # rescue raise Sinatra::NotFound.new
17
+ begin
18
+ view params['action']
19
+ rescue Errno::ENOENT
20
+ pass
21
+ end
7
22
  end
8
23
 
9
24
  not_found do
@@ -0,0 +1,14 @@
1
+ if config.locales.guess_from_hostname?
2
+
3
+ before do
4
+ default_locale = I18n.default_locale
5
+ request_locale = request.host.split(".").first.to_sym
6
+ locale = I18n.available_locales.include?( request_locale ) ? request_locale : default_locale
7
+ if locale == default_locale && config.hostname? && request.host != config.hostname
8
+ redirect current_url( locale: default_locale )
9
+ # flash[:debug] = "redirecto to canonical: #{current_url( locale: default_locale )}"
10
+ end
11
+ I18n.locale = locale
12
+ end
13
+
14
+ end # config.locales.guess_from_hostname?
@@ -0,0 +1,8 @@
1
+
2
+ locales {
3
+ guess_from_hostname true
4
+ }
5
+
6
+ aerogel {
7
+ reloader false
8
+ }
@@ -0,0 +1,9 @@
1
+ aerogel {
2
+ reloader true
3
+
4
+ cache {
5
+ enabled false
6
+ max_size 1_000
7
+ }
8
+
9
+ }
@@ -0,0 +1,10 @@
1
+ aerogel {
2
+
3
+ reloader false
4
+
5
+ cache {
6
+ enabled true
7
+ max_size 1_000
8
+ }
9
+
10
+ }