Class: ActionDispatch::Routing::Mapper

Inherits:
Object
  • Object
show all
Defined in:
lib/lolita/rails/railtie.rb,
lib/lolita/rails/routes.rb

Instance Method Summary collapse

Instance Method Details

#lolita_for(*resources) ⇒ Object

Every module, that is used with lolita and has routes, need to have resource method, for example, lolita_rest, that should be added to ActionDispatch::Routing::Mapper class, as a protected method. Module can automaticliy add resource route or allow user to do it. It accepts some useful options

  • :module

  • :path

  • :as

  • :path_prefix

  • :controller

  • :class_name

  • :singular

Example

Lolita.add_module Lolita::Gallery,:route=>:gallery
# in route.rb
lolita_for :galleries
# lolita_for try to call :lolita_gallery in Mapper class


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/lolita/rails/routes.rb', line 36

def lolita_for *resources
  options = resources.extract_options!

  options[:as]          ||= @scope[:as]     if @scope[:as].present?
  options[:module]      ||= @scope[:module] if @scope[:module].present?
  options[:path_prefix] ||= @scope[:path]   if @scope[:path].present?
  resources.map!(&:to_sym)
  all_resource_classes=[]
  resources.each{|resource|
    mapping = Lolita.add_mapping(resource,options)
    Lolita.resources[mapping.name] = mapping
    target_class = mapping.to

  #TODO refactor all these variables
    all_resource_classes<<target_class

    lolita_scope mapping.name do
      yield if block_given?

      with_lolita_exclusive_scope mapping.fullpath,mapping.path do

        # if not defined lolita default configuration in model, than can't use :rest

        # if !target_class.respond_to?(:lolita) && !Lolita::routes[mapping.name]
        #    raise Lolita::NotFound, "Lolita not found in #{target_class}. Include Lolita::Configuration"
        # elsif target_class.respond_to?(:lolita) && target_class.instance_variable_get(:@lolita).nil?
        #   warn("Lolita is not initialized, call `lolita` in #{target_class}")
        #   route = Lolita.routes[mapping.name] || Lolita.default_route
        #   #raise Lolita::NotInitialized, "Call lolita method in #{target_class}."
        # else
        #   route=Lolita.routes[mapping.name] || Lolita.default_route
        # end
        route = Lolita.routes[mapping.name] || Lolita.default_route
        unless route
          raise Lolita::ModuleNotFound, "Module #{mapping.name.to_s.capitalize} not found!"
        end
        send(:"lolita_#{route}_route",mapping,mapping.controllers)

        Lolita.conditional_routes(target_class).each do |route_name|
          send(:"lolita_#{route_name}_route",mapping,mapping.controllers)
        end
      end

    end

    mapping.add_to_navigation_tree
  }
  Lolita.common_routes(all_resource_classes).each do |route_name|
    send(:"lolita_#{route_name}_route")
  end
  # rescues sql exception when migration for new model is executed
rescue ActiveRecord::StatementInvalid
  Rails.logger.debug "Executing lolita_for got an error: #{$!}"
  nil
end