Class: Lolita::Mapping
Overview
Create mapping for routes. Each mapping has name, like :posts, :files etc. Also it accepts options:
-
:singular
- singular form for route, by default it call #singularize on name. -
:class_name
- class that is related with route, by default it uses :singular, and classify it. It should be like “Post”. -
:path_prefix
- path starts with path prefix, like /path_prefix/lolita/posts. -
:path
- path and path url methods starts with this path.
Example
lolita_for :posts, :path=>"admin"
# add paths like this to routes
# admin_posts GET /admin/posts {:controller=>"lolita/rest", :action=>:index}
# edit_admin_posts GET /admin/post/1/edit {:controller=>"lolita/rest",:action=>:edit}
-
:module
- change module for path, it changes :controller that is used for lolita, like,:module=>"admin"
, change controller to “admin/posts”. If this is used without :path then no named routes will be generated
Instances of this class is used all over the Lolita, this class itself represent what name does resource has, what controller to use, what model is related with it and so on. This is used to generate urls and paths. Also eahc request containers information with mapping related to it.
Instance Attribute Summary collapse
-
#append_to ⇒ Object
readonly
Returns the value of attribute append_to.
-
#as ⇒ Object
readonly
Returns the value of attribute as.
-
#class_name ⇒ Object
readonly
Returns the value of attribute class_name.
-
#controllers ⇒ Object
readonly
Returns the value of attribute controllers.
-
#module ⇒ Object
readonly
Returns the value of attribute module.
-
#only ⇒ Object
readonly
Returns the value of attribute only.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#path_prefix ⇒ Object
readonly
Returns the value of attribute path_prefix.
-
#plural ⇒ Object
readonly
Returns the value of attribute plural.
-
#singular ⇒ Object
(also: #name)
readonly
Returns the value of attribute singular.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
-
#visible ⇒ Object
readonly
Returns the value of attribute visible.
Instance Method Summary collapse
- #add_to_navigation_tree ⇒ Object
- #controller ⇒ Object
-
#fullpath ⇒ Object
full path of current mapping.
-
#initialize(name, options = {}) ⇒ Mapping
constructor
A new instance of Mapping.
-
#to ⇒ Object
Return class that is related with mapping.
-
#url_name ⇒ Object
TODO test what with namespace.
Constructor Details
#initialize(name, options = {}) ⇒ Mapping
Returns a new instance of Mapping.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/lolita/mapping.rb', line 25 def initialize(name,={}) # TODO how it is when lolita plugin extend default path and there is module is this not break the logic? @as = [:as] @title = [:title] @to = [:to].is_a?(String) ? [:to].constantize : [:to] @visible = .keys.include?(:visible) ? [:visible] : true @append_to = [:append_to] @only = [:only] || nil @plural = ([:as] ? [:as] : name).to_sym @singular = ([:singular] || @plural.to_s.singularize).to_sym @class_name = ([:class_name] || name.to_s.classify).to_s @ref = @class_name.to_s.camelize @path_prefix = [:path_prefix] @path = ([:path] || "lolita").to_s @module = [:module] @default_mod = @module || "lolita" @controllers = Hash.new{|h,k| h[k]=[:controller] || "#{!@module && "lolita/"}#{k}" } end |
Instance Attribute Details
#append_to ⇒ Object (readonly)
Returns the value of attribute append_to.
21 22 23 |
# File 'lib/lolita/mapping.rb', line 21 def append_to @append_to end |
#as ⇒ Object (readonly)
Returns the value of attribute as.
20 21 22 |
# File 'lib/lolita/mapping.rb', line 20 def as @as end |
#class_name ⇒ Object (readonly)
Returns the value of attribute class_name.
20 21 22 |
# File 'lib/lolita/mapping.rb', line 20 def class_name @class_name end |
#controllers ⇒ Object (readonly)
Returns the value of attribute controllers.
20 21 22 |
# File 'lib/lolita/mapping.rb', line 20 def controllers @controllers end |
#module ⇒ Object (readonly)
Returns the value of attribute module.
20 21 22 |
# File 'lib/lolita/mapping.rb', line 20 def module @module end |
#only ⇒ Object (readonly)
Returns the value of attribute only.
21 22 23 |
# File 'lib/lolita/mapping.rb', line 21 def only @only end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
20 21 22 |
# File 'lib/lolita/mapping.rb', line 20 def path @path end |
#path_prefix ⇒ Object (readonly)
Returns the value of attribute path_prefix.
20 21 22 |
# File 'lib/lolita/mapping.rb', line 20 def path_prefix @path_prefix end |
#plural ⇒ Object (readonly)
Returns the value of attribute plural.
20 21 22 |
# File 'lib/lolita/mapping.rb', line 20 def plural @plural end |
#singular ⇒ Object (readonly) Also known as: name
Returns the value of attribute singular.
20 21 22 |
# File 'lib/lolita/mapping.rb', line 20 def singular @singular end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
21 22 23 |
# File 'lib/lolita/mapping.rb', line 21 def title @title end |
#visible ⇒ Object (readonly)
Returns the value of attribute visible.
21 22 23 |
# File 'lib/lolita/mapping.rb', line 21 def visible @visible end |
Instance Method Details
#add_to_navigation_tree ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/lolita/mapping.rb', line 64 def tree = Lolita. if self.visible if self.append_to parent_branch = tree.branches.detect{|b| b.[:system_name] == self.append_to} unless parent_branch parent_branch = tree.append(nil,:title => lambda{|branch| return ::I18n.t("lolita.navigation." + branch.[:system_name]) }, :system_name => self.append_to ) end tree = parent_branch.children end unless tree.branches.detect{|b| b.object.is_a?(Lolita::Mapping) && b.object.to==self.to} tree.append(self, :title => @title) end end end |
#controller ⇒ Object
46 47 48 |
# File 'lib/lolita/mapping.rb', line 46 def controller "#{@default_mod}#{@default_mod && "/"}#{@plural}" end |
#fullpath ⇒ Object
full path of current mapping
56 57 58 |
# File 'lib/lolita/mapping.rb', line 56 def fullpath "#{@path_prefix}/#{@path}".squeeze("/") end |
#to ⇒ Object
Return class that is related with mapping.
51 52 53 |
# File 'lib/lolita/mapping.rb', line 51 def to @to || (@ref.constantize rescue nil) end |
#url_name ⇒ Object
TODO test what with namespace
60 61 62 |
# File 'lib/lolita/mapping.rb', line 60 def url_name #TODO test what with namespace "#{@path}_#{@plural}" end |