Skip to content

Instantly share code, notes, and snippets.

@urbed
Last active March 9, 2017 12:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save urbed/de4a1764ca33f8e4bc432c05b1ba3d58 to your computer and use it in GitHub Desktop.
Save urbed/de4a1764ca33f8e4bc432c05b1ba3d58 to your computer and use it in GitHub Desktop.
Any comments please?

I've been hacking up a wiki for the display of computer artefacts in a museum-like way, with interested people being able to comment easily on items displayed in this virtual museum. In this way a corpus of knowledge may develop around the items displayed on the wiki.

Collection items in the wiki can be categorised using a categories field on each page for the collection item.

Categories exist in a simple hierarchy. In the wiki running at markvan.me I have this fragment of a category hierarchy:

Manchester Computer -isa-> Computer

In the running demo, I have one page categoriesed with 'Computer', and one categorised with 'Manchester Computer'

The page below shows things categorised 'Computer' https://markvan.me/categories/Computer

By clicking on the show subcategories button you see something categorised with a subcategory of 'Computer' https://markvan.me/categories/Computer/show_full

The categories controller, freshly refactored, is at https://github.com/markvan/social-museum/blob/refactor-bootstrap2/app/controllers/categories_controller.rb

Looking at a fragment of that controller in the next file

def get_categorized(clazz)
cat = params[:id]
cat_all = params[:category_id]
cat ? Category.find_categorized(cat, clazz) : Category.find_categorized_including_child_categories(cat_all, clazz)
end
expose(:categorised_collection_items) { get_categorized(CollectionItem) }
expose(:categorised_pages) { get_categorized(Page) }
expose(:categorised_resources) { get_categorized(Resource) }

I dont like that I had to write method get_categorised in the controller while refactoring it, but it removed a lot of mess inthe controller (mess with too many lines of expose method invocations).

Certainly the tenary operator at line 4 shown above above simplifies the code on lines 7-9.

Let me dig into that method: The method lines 2 and 3's use of ':id' and 'category_id' are a consequence of the URLs shown in this fragment of a routing error output page

  GET	 /categories/:category_id/show_full(.:format)	  categories#show_full
  GET	 /categories/:id(.:format)	                    categories#show

And in turn a consequence of this fragment of routes.rb

  resources :categories, only: :show do
     get :show_full
  end
  1. Im reasonably happy to live with the method in the controller, unless you say otherwise.

  2. I've looked in vain for an authorative answer for the 'neater syntax' part of this: If I wanted a categories controller with simpy a param[:id] for both URLs below, what would I write in routes.rb please? I reckon this involves writing two GET directives in a particular order in routes.rb, but is there a neater syntax?

  GET /categories/:id/show_full    categories#show_full
  GET	 /categories/:id             categories#show
  1. So I believe I'm doing three (N+1) queries to display that page. If I was concerned about performance (one day I will be) what lines of attack do I have to reduce a performance hit?

Thanks!

@Pavling
Copy link

Pavling commented Mar 9, 2017

resources :categories, only: :show do
  member do
    get :show_full
  end
end

That should give you the params[:id] for both routes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment