Module: Trek::Model

Extended by:
ActiveSupport::Concern
Included in:
ResourceController
Defined in:
app/controllers/concerns/trek/model.rb

Instance Method Summary collapse

Instance Method Details

#modelObject



11
12
13
# File 'app/controllers/concerns/trek/model.rb', line 11

def model
  raise "Please define a `model` method in your controller."
end

#model_collectionObject



19
20
21
# File 'app/controllers/concerns/trek/model.rb', line 19

def model_collection
  model_name.collection.to_sym
end

#model_collection_routeObject

Returns model collection as an array of symbols for route building. For namespaced models like Shop::Product, returns [:shop, :products]. For non-namespaced models like Page, returns [:pages].



26
27
28
# File 'app/controllers/concerns/trek/model.rb', line 26

def model_collection_route
  model_name.collection.split("/").map(&:to_sym)
end

#model_elementObject



30
31
32
# File 'app/controllers/concerns/trek/model.rb', line 30

def model_element
  model_name.element.to_sym
end

#model_element_routeObject

Returns model element as an array of symbols for route building. For non-namespaced models like Page, returns [:page]. For namespaced models, it depends on the route key: callers build member routes as [:admin, *model_element_route[0...-1], record], and the record's route key must not carry the namespace twice.

  • Shop::Product (plain app namespace, route key shop_product) returns [:shop_product] — a [:shop, :product] prefix would yield admin_shop_shop_product_path.
  • Blog::Post from an isolated engine (isolate_namespace → relative model naming, route key post) returns [:blog, :post], since the namespace segment is needed for admin_blog_post_path.


45
46
47
48
49
# File 'app/controllers/concerns/trek/model.rb', line 45

def model_element_route
  element = model_name.singular_route_key
  namespace = element == model_name.element ? model_name.collection.split("/")[0...-1] : []
  (namespace + [element]).map(&:to_sym)
end

#model_intro(action: nil) ⇒ Object



51
52
53
54
# File 'app/controllers/concerns/trek/model.rb', line 51

def model_intro(action: nil)
  action ||= @object&.new_record? ? "new" : "edit"
  t("admin.#{model_collection}.#{action}.intro")
end

#model_objectsObject



15
16
17
# File 'app/controllers/concerns/trek/model.rb', line 15

def model_objects
  model.all
end