Class: Lolita::BaseConfiguration

Inherits:
Object
  • Object
show all
Defined in:
lib/lolita/base_configuration.rb

Overview

Base::Configuration methods are accessable through Lolita module. Like Lolita.modules and Lolita.routes and so on.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scope) ⇒ BaseConfiguration

Returns a new instance of BaseConfiguration.



10
11
12
13
14
15
16
17
18
19
# File 'lib/lolita/base_configuration.rb', line 10

def initialize(scope)
  @scope=scope
  @mappings={}
  @resources={}
  @default_module=nil
  @user_classes=[]
  @modules=[]
  @routes={}
  @controllers={}
end

Instance Attribute Details

#authenticationObject

Returns the value of attribute authentication.



7
8
9
# File 'lib/lolita/base_configuration.rb', line 7

def authentication
  @authentication
end

#controllersObject (readonly)

Returns the value of attribute controllers.



6
7
8
# File 'lib/lolita/base_configuration.rb', line 6

def controllers
  @controllers
end

#default_localeObject

Return default locale. First looks for defined default locale for Lolita, when not found than take first of defined #locales for Lolita, if there no defined locales for Lolita, than look for I18n and take default locale from there or if there is no I18n than take :en



52
53
54
# File 'lib/lolita/base_configuration.rb', line 52

def default_locale
  @default_locale || self.locales.first || (defined?(I18n) ? I18n.default_locale : :en)
end

#default_routeObject

Returns the value of attribute default_route.



7
8
9
# File 'lib/lolita/base_configuration.rb', line 7

def default_route
  @default_route
end

#mappingsObject

Returns the value of attribute mappings.



7
8
9
# File 'lib/lolita/base_configuration.rb', line 7

def mappings
  @mappings
end

#modulesObject (readonly)

Returns the value of attribute modules.



6
7
8
# File 'lib/lolita/base_configuration.rb', line 6

def modules
  @modules
end

#resourcesObject (readonly)

Returns the value of attribute resources.



6
7
8
# File 'lib/lolita/base_configuration.rb', line 6

def resources
  @resources
end

#routesObject (readonly)

Returns the value of attribute routes.



6
7
8
# File 'lib/lolita/base_configuration.rb', line 6

def routes
  @routes
end

#scopeObject (readonly)

Returns the value of attribute scope.



6
7
8
# File 'lib/lolita/base_configuration.rb', line 6

def scope
  @scope
end

#user_classesObject

Returns the value of attribute user_classes.



7
8
9
# File 'lib/lolita/base_configuration.rb', line 7

def user_classes
  @user_classes
end

Instance Method Details

#add_mapping(resource, options = {}) ⇒ Object



91
92
93
94
95
# File 'lib/lolita/base_configuration.rb', line 91

def add_mapping(resource,options={})
  mapping = Lolita::Mapping.new(resource, options)
  self.mappings[mapping.name] = mapping
  mapping
end

#add_module(module_container, options = {}) ⇒ Object

Add new module to Lolita Accpted options

  • controller - not in use

  • nested - is route stands itsefl or is used in combination with resource

  • route - Symbol of route name or lambad, that return route name based on resource.

Route name is used to call method lolita_ in Mapper class, and that should draw route.

  • :name - name of module, underscored symbol. Used to draw default route, by default always

lolita_rest is called, but if route with resource name is found, than by default this route will be drawn. Like lolita_for :posts, can go to different controller than rest and do other things.

  • :path - some file that will be included. Deprecated will be removed

Example

Lolita.add_module Lolita::Posts, :route=>:post, :name=>:post
lolita_for :posts #=> create url whatever is defined in lolita_post method, and goes to :controller=>"lolita/posts"
Lolita.add_module Lolita::FileUpload, :route=>lambda{|resource| resource.lolita.tabs.by_type(:file) ? :file_upload : nil}
lolita_for :users #=> creat default rest urls and also call method lolita_file_upload if user lolita define :file tab.

To add route for public interface that goes to added module, than use

Lolita.add_module Post, :name=>:posts

And then when in routes.rb will be defined lolita_for(:posts) it will call method lolita_posts_route and that method should define resource.

Example

# require this in your gem or lib
module ActionDispatch::Routing
   class Mapper
     protected
     def lolita_posts_route mapping, controllers
       resources mapping.plural,:only=>[:index,:new,:create],
         :controller=>controllers[:posts],:module=>mapping.module
     end
   end
 end

You open Mapper class and add your method that call #resources or #match or other method that define route For common route for all lolita resources your method should look like this

Example

def lolita_files_route 
   mapping=Lolita.add_mapping(:files,:class_name=>"Lolita::Multimedia::File",:module=>"file_upload")
   scope :module=>mapping.module do
       resources mapping.name
   end
end


136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/lolita/base_configuration.rb', line 136

def add_module module_container, options={}
  options.assert_valid_keys(:controller,:route,:model,:path,:name,:nested)
  name=options[:name]||module_container.to_s.to_sym
  self.modules<<module_container

  self.routes[name]=[options.has_key?(:nested) ? options[:nested] : true,options[:route]]
  self.controllers[name]=options[:controller] if options.has_key?(:controller)

  if options[:path]
    require File.join(options[:path],name.to_s)
  end
  
end

#common_routes(klasses) ⇒ Object

Find all routes that is needed for defined classes And return only one for each different route.



78
79
80
81
82
83
84
# File 'lib/lolita/base_configuration.rb', line 78

def common_routes(klasses)
  @routes.map{|name,route|
    unless route.first
      klasses.map{|klass| route.last.respond_to?(:call) ? route.last.call(klass) : route.last}
    end
  }.flatten.compact.uniq
end

#conditional_routes(klass = nil) ⇒ Object

Call (with #call) to route klass And return all names of routes that are needed for resource. When with #add_module routes are defined like

Lolita.add_module MyModule, :route=>:my_module

then this will be passed to the method that creates routes, but when Proc is passed to :route then this Proc should return name of route or nil. These names then are used for methods like lolita_[route_name]_route that should be required somewhere in you module.



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/lolita/base_configuration.rb', line 64

def conditional_routes(klass=nil)
  @routes.map{|name,route|
    if route.first
      if route.last.respond_to?(:call)
        route.last.call(klass)
      else
        route.last
      end
    end
  }.compact
end

#localeObject



38
39
40
# File 'lib/lolita/base_configuration.rb', line 38

def locale()
  @locale || default_locale
end

#locale=(given_locale) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/lolita/base_configuration.rb', line 42

def locale=given_locale
  @locale=if locales.include?(given_locale.to_s.to_sym)
    given_locale.to_s.to_sym
  else
    Lolita.default_locale
  end
end

#localesObject



34
35
36
# File 'lib/lolita/base_configuration.rb', line 34

def locales
  @locales || []
end

#locales=(value) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/lolita/base_configuration.rb', line 26

def locales=(value)
  unless value.is_a?(Array)
    @locales=[value]
  else
    @locales=value
  end
end

#lolita_extend_route_forObject

Lolita.extend_route_for(:any,:posts,:file_upload) do |resource|

if resource.tabs.by_type(:metadata)
  with do
    resources :metadata
  end 
end

end :any for any lolita/posts/metadata lolita/posts/files/metadata



160
161
# File 'lib/lolita/base_configuration.rb', line 160

def lolita_extend_route_for()
end


21
22
23
24
# File 'lib/lolita/base_configuration.rb', line 21

def navigation
  @navigation||=Lolita::Navigation::Base.new()
  @navigation
end

#use(module_name) ⇒ Object

Include module in Lolita, don’t know why i need this



87
88
89
# File 'lib/lolita/base_configuration.rb', line 87

def use(module_name)
  Lolita.send(:include,module_name)
end