Class: Lolita::SystemConfiguration::Base

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scope) ⇒ Base

Returns a new instance of Base.



8
9
10
11
12
13
14
15
16
17
# File 'lib/lolita/system_configuration/base.rb', line 8

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

Instance Attribute Details

#ability_classObject



35
36
37
# File 'lib/lolita/system_configuration/base.rb', line 35

def ability_class
  @ability_class || (::Ability rescue nil) || raise("No ability class found.")
end

#authenticationObject

Returns the value of attribute authentication.



5
6
7
# File 'lib/lolita/system_configuration/base.rb', line 5

def authentication
  @authentication
end

#authorizationObject

Returns the value of attribute authorization.



5
6
7
# File 'lib/lolita/system_configuration/base.rb', line 5

def authorization
  @authorization
end

#controllersObject (readonly)

Returns the value of attribute controllers.



4
5
6
# File 'lib/lolita/system_configuration/base.rb', line 4

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



75
76
77
# File 'lib/lolita/system_configuration/base.rb', line 75

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

#default_routeObject

Returns the value of attribute default_route.



5
6
7
# File 'lib/lolita/system_configuration/base.rb', line 5

def default_route
  @default_route
end

#mappingsObject

Returns the value of attribute mappings.



5
6
7
# File 'lib/lolita/system_configuration/base.rb', line 5

def mappings
  @mappings
end

#modulesObject (readonly)

Returns the value of attribute modules.



4
5
6
# File 'lib/lolita/system_configuration/base.rb', line 4

def modules
  @modules
end

#policy_classObject

policy class for Pundit



40
41
42
# File 'lib/lolita/system_configuration/base.rb', line 40

def policy_class
  @policy_class || (::LolitaPolicy rescue nil) || raise("No policy class found.")
end

#resourcesObject (readonly)

Returns the value of attribute resources.



4
5
6
# File 'lib/lolita/system_configuration/base.rb', line 4

def resources
  @resources
end

#routesObject (readonly)

Returns the value of attribute routes.



4
5
6
# File 'lib/lolita/system_configuration/base.rb', line 4

def routes
  @routes
end

#scopeObject (readonly)

Returns the value of attribute scope.



4
5
6
# File 'lib/lolita/system_configuration/base.rb', line 4

def scope
  @scope
end

#tinymce_configuration_setObject

Should return one of defined configuration set names from tinymce.yml



177
178
179
# File 'lib/lolita/system_configuration/base.rb', line 177

def tinymce_configuration_set
  @tinymce_configuration_set || :default
end

#user_classesObject

Returns the value of attribute user_classes.



5
6
7
# File 'lib/lolita/system_configuration/base.rb', line 5

def user_classes
  @user_classes
end

Instance Method Details

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



115
116
117
118
119
# File 'lib/lolita/system_configuration/base.rb', line 115

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

Raises:

  • (ArgumentError)


160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/lolita/system_configuration/base.rb', line 160

def add_module module_container, options={}
  raise ArgumentError, "Can't add module without module container!" unless module_container
  options.assert_valid_keys(:controller,:route,:model,:path,:name,:nested)
  name=options[:name]||module_container.to_s.to_sym
  self.modules<<module_container

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

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

#application(&block) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/lolita/system_configuration/base.rb', line 19

def application &block
  @application ||= Lolita::SystemConfiguration::Application.new
  if block_given?
    yield @application
  end
  @application
end

#common_routes(klasses) ⇒ Object

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



102
103
104
105
106
107
108
# File 'lib/lolita/system_configuration/base.rb', line 102

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.



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/lolita/system_configuration/base.rb', line 88

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



56
57
58
# File 'lib/lolita/system_configuration/base.rb', line 56

def locale()
  @locale || default_locale
end

#locale=(given_locale) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/lolita/system_configuration/base.rb', line 60

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
  if defined?(::I18n)
    ::I18n.locale = @locale
  end
  @locale
end

#localesObject



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

def locales
  @locales || []
end

#locales=(value) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/lolita/system_configuration/base.rb', line 44

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


27
28
29
30
31
32
33
# File 'lib/lolita/system_configuration/base.rb', line 27

def navigation
  unless Lolita::Navigation::Tree[:"left_side_navigation"]
    tree = Lolita::Navigation::Tree.new(:"left_side_navigation")
    Lolita::Navigation::Tree.remember(tree)
  end
  Lolita::Navigation::Tree[:"left_side_navigation"]
end

#use(module_name) ⇒ Object

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



111
112
113
# File 'lib/lolita/system_configuration/base.rb', line 111

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