Class: Menuable::Menu
- Inherits:
-
Object
- Object
- Menuable::Menu
- Defined in:
- lib/menuable.rb
Overview
The actual state of the definition body that reads YAML
Instance Method Summary collapse
- #all ⇒ Object
- #call(context) ⇒ Object
- #first(current_user:) ⇒ Object
-
#initialize(namespace, path) ⇒ Menu
constructor
rubocop:todo Metrics/MethodLength, Metrics/AbcSize, Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity.
-
#routes(routing) ⇒ Object
rubocop:todo Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/AbcSize, Metrics/PerceivedComplexity.
Constructor Details
#initialize(namespace, path) ⇒ Menu
rubocop:todo Metrics/MethodLength, Metrics/AbcSize, Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/menuable.rb', line 132 def initialize(namespace, path) # rubocop:todo Metrics/MethodLength, Metrics/AbcSize, Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity extract_namespace = lambda do |name| if name namespaces = name.split("/") namespaces.pop namespaces else [] end end @controllers = {} = YAML.load_file(path).map do |config| config.deep_symbolize_keys! namespaces = extract_namespace.call(config[:name]) controller = "#{namespace}/#{config[:name]&.pluralize}_controller".classify.safe_constantize @controllers[namespaces] ||= [] @controllers[namespaces] << controller if controller config[:items]&.each do |item| item[:controller] = "#{namespace}/#{item[:name]&.pluralize}_controller".classify.safe_constantize item_namespaces = extract_namespace.call(item[:name]) @controllers[item_namespaces] ||= [] @controllers[item_namespaces] << item[:controller] if item[:controller] end { **config, controller:, namespaces: } end end |
Instance Method Details
#all ⇒ Object
162 163 164 |
# File 'lib/menuable.rb', line 162 def all end |
#call(context) ⇒ Object
166 167 168 |
# File 'lib/menuable.rb', line 166 def call(context) MenuContext.new(menus: , context:) end |
#first(current_user:) ⇒ Object
170 171 172 173 174 |
# File 'lib/menuable.rb', line 170 def first(current_user:) @controllers.values.flatten.each do |controller| break controller. if controller..loyalty.call(current_user) end end |
#routes(routing) ⇒ Object
rubocop:todo Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/AbcSize, Metrics/PerceivedComplexity
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/menuable.rb', line 176 def routes(routing) # rubocop:todo Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/AbcSize, Metrics/PerceivedComplexity controller_mappings = @controllers routing.instance_eval do # rubocop:todo Metrics/BlockLength controller_mappings.each do |namespaces, controllers| # rubocop:todo Metrics/BlockLength define = controllers.map do |controller| if controller..single? lambda do |router| router.resource controller..resource_name do router.instance_eval(&controller..actions) if controller..actions end end else lambda do |router| router.resources controller..resource_name do router.instance_eval(&controller..actions) if controller..actions end end end end case namespaces.length when 3 namespace namespaces[0] do namespace namespaces[1] do namespace namespaces[2] { define.each { _1.call(self) } } end end when 2 namespace namespaces[0] do namespace(namespaces[1]) { define.each { _1.call(self) } } end when 1 namespace(namespaces[0]) { define.each { _1.call(self) } } when 0 define.each { _1.call(self) } end end end end |