Module: BulletTrain::Routes::MapperExtensions
- Defined in:
- lib/bullet_train/routes/mapper_extensions.rb
Instance Method Summary collapse
- #current_models ⇒ Object
- #current_namespaces ⇒ Object
- #deduplicate_namespaces(namespace_names) ⇒ Object
- #model(*parameters, &block) ⇒ Object
- #namespaces(namespaces, &block) ⇒ Object
- #namespaces_to_eject(namespace_names) ⇒ Object
Instance Method Details
#current_models ⇒ Object
8 9 10 |
# File 'lib/bullet_train/routes/mapper_extensions.rb', line 8 def current_models @current_models ||= [] end |
#current_namespaces ⇒ Object
4 5 6 |
# File 'lib/bullet_train/routes/mapper_extensions.rb', line 4 def current_namespaces @current_namespaces ||= [] end |
#deduplicate_namespaces(namespace_names) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/bullet_train/routes/mapper_extensions.rb', line 24 def deduplicate_namespaces(namespace_names) still_needed = [] while namespace_names.any? if current_namespaces.last(namespace_names.count).map { |namespace| namespace[:namespace_name] } == namespace_names return still_needed else still_needed.unshift namespace_names.pop end end still_needed end |
#model(*parameters, &block) ⇒ Object
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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/bullet_train/routes/mapper_extensions.rb', line 54 def model(*parameters, &block) namespace_names = parameters[0].underscore.split("/") current_models << {model_name: namespace_names.dup, pending_blocks: []} model_name = namespace_names.pop original_namespace_names = namespace_names.dup namespace_names = deduplicate_namespaces(namespace_names) # E.g. `Project` vs. `Projects::Deliverable`. if current_models[-2] && namespace_names.first == current_models[-2][:model_name].last.pluralize current_namespaces << {namespace_name: namespace_names.shift, pending_blocks: []} scope module: current_namespaces.last[:namespace_name] do namespaces namespace_names do # TODO We need people to be able to specify more than just [:index, :new, :create] for collection actions. resources model_name.pluralize.to_sym, (parameters[1] || {}).merge(only: [:index, :new, :create]), &block end end current_namespaces_last = current_namespaces.last[:namespace_name] current_models.last[:pending_blocks] << proc do # This needs to be registered to run _after_ we leave the `resources` block of the parent. namespace current_namespaces_last.to_sym do resources model_name.pluralize.to_sym, (parameters[1] || {}).merge(except: [:index, :new, :create]), &block end end current_namespaces.pop[:pending_blocks].each do |block| block.call end # E.g. `Projects::Deliverable` and `Objective` elsif (ejection_count = namespaces_to_eject(original_namespace_names).count) > 0 resource_symbol = current_models[-2][:model_name][((ejection_count + 1) * -1)..-1].join("_").to_sym resource_path = current_models[-2][:model_name][((ejection_count + 1) * -1)..-1].join("/") current_namespaces[ejection_count * -1][:pending_blocks] << proc do resources resource_symbol, path: resource_path do namespaces namespace_names do resources model_name.pluralize.to_sym, parameters[1] || {}, &block end end end else namespaces namespace_names do resources model_name.pluralize.to_sym, parameters[1] || {}, &block end end current_models.pop[:pending_blocks].each do |block| block.call end end |
#namespaces(namespaces, &block) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/bullet_train/routes/mapper_extensions.rb', line 12 def namespaces(namespaces, &block) if namespaces.any? current_namespaces << {namespace_name: namespaces.shift, pending_blocks: []} namespace current_namespaces.last[:namespace_name], &block current_namespaces.pop[:pending_blocks].each do |block| block.call end else yield block end end |
#namespaces_to_eject(namespace_names) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/bullet_train/routes/mapper_extensions.rb', line 38 def namespaces_to_eject(namespace_names) return [] unless current_models[-2] # E.g. `Common::Projects::Deliverable` and `Common::Objective` should be 1. # E.g. `Common::Projects::Deliverable` and `Common::Other::Objective` should be 1. working_namespace_names = namespace_names.dup parent_namespace_names = current_models[-2][:model_name][0..-2] while parent_namespace_names.any? && working_namespace_names.first == parent_namespace_names.first working_namespace_names.shift parent_namespace_names.shift end parent_namespace_names end |