These are chat archives for hanami/chat

14th
Feb 2017
Dohyung Ahn
@adhrinae
Feb 14 2017 06:15 UTC
@gizotti Um.. I've always used Hash for updating entities. Like PostRepository.new.update(post.id, title: 'New Title')
Gabriel Gizotti
@gizotti
Feb 14 2017 06:20 UTC
@emaren84 I guessed that was the case. It's not a problem, I was just curious if there was any special way of doing it or not :)
Michael Canden-Lennox
@michaellennox
Feb 14 2017 09:04 UTC

hey people, I’m currently starting a new hanami project that will contain a couple of jekyll sites. I’ve got a pipeline at the moment that takes the jekyll source from an application in the apps directory and builds it to a directory of the same name in /public.

My assumption is this is going to work happily in production because hanami won’t be serving the static assets but in development it doesn’t seem to infer that the path should evaluate to the index.html file

For example I have a jekyll app called ‘guides’. This is built to /public/guides which contains an index.html file (plus various other subdirectories containing the static assets for that jekyll site). Now what I would expect is by doing GET /guides it would serve me the index.html file from that directory but it doesn’t, I have to explicitly do GET /guides/index.html to reach the site.

Is there a way to make hanami when serving static assets in development/test environments infer that /guides should infer /public/guides/index.html by default?

Kai Kuchenbecker
@kaikuchn
Feb 14 2017 09:25 UTC
@michaellennox in development those static files are served by Hanami::Static which is a thin wrapper around Rack::Static. It is there that one would have to add mappings for index.html files. E.g., currently the hash returned from the urls method may look like this: { '/guides/index.html' => 'guides/index.html' } while you'd need it to look like that: {'/guides/index.html' => 'guides/index.html', '/guides' => 'guides/index.html'}.
This could be achieved by monkey patching, or you could just handle your static files serving needs in development by plucking in a middleware for that.
Anyway, I don't know of any configuration option that would enable what you need, if that was your question.
Maybe this is something that should be possible though, take it up with the maintainers and see what they think about it :)
Michael Canden-Lennox
@michaellennox
Feb 14 2017 09:30 UTC
thanks @kaikuchn, for now I guess I’ll go poke around and see about monkey patching it. Personally I think the middleware should evaluate a directory to it’s index.html file, this is pretty much what anything you use in production would do (and is also the default for most other frameworks). Would be interested to hear maintainers thoughts on that
Kai Kuchenbecker
@kaikuchn
Feb 14 2017 09:30 UTC
Oh, it's also enough to add the index option to the Rack::Static initializer, given your paths end in a /
def initialize(app, root: Hanami.public_directory, header_rules: HEADER_RULES)
  super(app, urls: _urls(root), root: root, header_rules: header_rules, index: 'index.html')
end
should do the trick
Sorry, I meant to say "it is also enough to just add the index option when calling the Rack::Static initializer"
Michael Canden-Lennox
@michaellennox
Feb 14 2017 09:41 UTC
where would you put that in the codebase? I’ve tried opening up the Hanami::Static class to monkey patch the initializer in multiple places in an initializer then the environment.rb file but it doesn’t seem to actually get run anywhere
Kai Kuchenbecker
@kaikuchn
Feb 14 2017 09:48 UTC
I'd require the monkey-patch in the apps/web/application.rb
I think this is a sensible place, since the patch pertains to your web-delivery mechanism.
Kai Kuchenbecker
@kaikuchn
Feb 14 2017 10:12 UTC
Could be that that's too late though. I'm not quite sure at what point the Hanami::Static middleware is initialized.
Gabriel Gizotti
@gizotti
Feb 14 2017 10:16 UTC
Hey guys, how would I go by ordering ASC instead of DESC my query in a repo?
Michael Canden-Lennox
@michaellennox
Feb 14 2017 10:19 UTC

ah so at the moment the actual jekyll sites aren’t hanami apps, they are just jekyll source code that builds to the public directory. As there’s nothing dynamic about them whatsoever I’d rather keep hanami as out of the way as possible.

I suspect that would be too late though even if they were hanami apps as I’ve tried adding the monkey patch multiple places both earlier and later than the apps being required into environment.rb and unfortunately no luck as of yet

Kai Kuchenbecker
@kaikuchn
Feb 14 2017 10:31 UTC
Well if you put the patch at the top of the config/environment.rb file and then start the server via rackup it should definitely get loaded. Can you show me how you tried to load the patch and what your patch looks like?
Michael Canden-Lennox
@michaellennox
Feb 14 2017 10:40 UTC
require 'rack/static'

module Hanami
  class Static < ::Rack::Static
    def initialize(app, root: Hanami.public_directory, header_rules: HEADER_RULES)
      puts 'testing this is run'
      super(app, urls: _urls(root), root: root, header_rules: header_rules, index: 'index.html')
    end
  end
end
that was placed at the top of config/environment.rb, serving both with rackup and hanami serve doesn’t seem to actually get called
Kai Kuchenbecker
@kaikuchn
Feb 14 2017 10:54 UTC
ah, yes it will get overwritten when Hanami::Static is actually loaded
just put a require 'hanami/static' before the patch
and it shouldn't get overwritten
Michael Canden-Lennox
@michaellennox
Feb 14 2017 10:58 UTC
ah yes it works! silly me
thanks for the help @kaikuchn
ah, one problem is that now appears to overwrite all paths to try serving an index file from public
eg GET / instead of going to my landing page served by hanami is now attempting to serve public/index.html
Kai Kuchenbecker
@kaikuchn
Feb 14 2017 11:00 UTC
Yes, that is solved on master
The static file server precluded the router in versions prior to 1.0.0.beta
even for non-existing static files
if you don't want to update, just expand your patch with this:
    private

    def _urls(root)
      return [] unless root.exist?

      asset_files = Dir.chdir(root) do
        Dir['**/*'].select { |path| File.file? path }
      end
      Hash[asset_files.map { |entry| ["#{URL_PREFIX}#{entry}", entry] }]
    end
Michael Canden-Lennox
@michaellennox
Feb 14 2017 11:04 UTC
issue still seems to be present when adding that to the patch
Kai Kuchenbecker
@kaikuchn
Feb 14 2017 11:05 UTC
then there is an index.html file at your public folders root
Michael Canden-Lennox
@michaellennox
Feb 14 2017 11:05 UTC
nope definitely not, it’s returning a 404 'File not found: /index.html'
$ ls public
assets guides
Kai Kuchenbecker
@kaikuchn
Feb 14 2017 11:06 UTC
Ah crap, you're right.
Kai Kuchenbecker
@kaikuchn
Feb 14 2017 11:11 UTC
when providing the :index option Rack::Static will always claim to be able to serve a path that ends in a /..
See here
And since Hanami::Assets::Static asks Rack::Static first whether or not we should serve a static asset for a request, we do try and then fail.
Michael Canden-Lennox
@michaellennox
Feb 14 2017 11:14 UTC
ah
Kai Kuchenbecker
@kaikuchn
Feb 14 2017 11:14 UTC
So I guess it's no good to specify the index option. Instead build a hash in the _urls method as I've initially suggested.
Damn, I thought we could solve this beautifully by using the index option :disappointed:
Michael Canden-Lennox
@michaellennox
Feb 14 2017 11:15 UTC
ahha that would be nice :)
Michael Canden-Lennox
@michaellennox
Feb 14 2017 11:27 UTC

ok so extended the hash that’s built as follows

Hash[asset_files.map { |entry| ["#{URL_PREFIX}#{entry}", entry] }]
        .merge('/guides'=>'guides/index.html')

now this works quite happily for GET /guides but I don’t seem to be able to make it happily handle visiting /guides/, if I add a mapper in there such as ‘/guides/'=>'guides/index.html’ it errors with the following stack trace when visiting /guides/

TypeError: no implicit conversion of nil into String
    /Users/Michael/.rvm/gems/ruby-2.4.0@bootcamphub/gems/hanami-0.9.2/lib/hanami/assets/static.rb:80:in `+'
    /Users/Michael/.rvm/gems/ruby-2.4.0@bootcamphub/gems/hanami-0.9.2/lib/hanami/assets/static.rb:80:in `serve'
    /Users/Michael/.rvm/gems/ruby-2.4.0@bootcamphub/gems/hanami-0.9.2/lib/hanami/assets/static.rb:47:in `call'
    /Users/Michael/.rvm/gems/ruby-2.4.0@bootcamphub/gems/rack-1.6.5/lib/rack/lint.rb:49:in `_call'
    /Users/Michael/.rvm/gems/ruby-2.4.0@bootcamphub/gems/rack-1.6.5/lib/rack/lint.rb:37:in `call'
    /Users/Michael/.rvm/gems/ruby-2.4.0@bootcamphub/gems/rack-1.6.5/lib/rack/showexceptions.rb:24:in `call'
    /Users/Michael/.rvm/gems/ruby-2.4.0@bootcamphub/gems/rack-1.6.5/lib/rack/commonlogger.rb:33:in `call'
    /Users/Michael/.rvm/gems/ruby-2.4.0@bootcamphub/gems/rack-1.6.5/lib/rack/content_length.rb:15:in `call'
    /Users/Michael/.rvm/gems/ruby-2.4.0@bootcamphub/gems/rack-1.6.5/lib/rack/handler/webrick.rb:88:in `service'
    /Users/Michael/.rvm/rubies/ruby-2.4.0/lib/ruby/2.4.0/webrick/httpserver.rb:140:in `service'
    /Users/Michael/.rvm/rubies/ruby-2.4.0/lib/ruby/2.4.0/webrick/httpserver.rb:96:in `run'
    /Users/Michael/.rvm/rubies/ruby-2.4.0/lib/ruby/2.4.0/webrick/server.rb:290:in `block in start_thread’

Not the end of the world but any quick ideas?

Kai Kuchenbecker
@kaikuchn
Feb 14 2017 12:59 UTC
notice how this check differs from the one used in rack.
Actually for your particular use case it would be easiest to just return an array of urls instead of a hash from the _url method, i think.
This message was deleted
Kai Kuchenbecker
@kaikuchn
Feb 14 2017 13:18 UTC
This is getting really ugly quickly.. you'd have to patch Hanami::Assets::Static to reflect rack/rack@5db439f
Michael Canden-Lennox
@michaellennox
Feb 14 2017 13:27 UTC
as you say, yes getting ugly quickly! That should be easy enough to patch though.
Armin
@wuarmin
Feb 14 2017 14:52 UTC
Congrats! :smile:
Alfonso Uceda
@AlfonsoUceda
Feb 14 2017 14:52 UTC
:clap:
Luca Guidi
@jodosha
Feb 14 2017 14:52 UTC
@wuarmin @AlfonsoUceda Thank you :)
Kai Kuchenbecker
@kaikuchn
Feb 14 2017 15:28 UTC
:heart:
The Crab
@theCrab
Feb 14 2017 15:43 UTC
@jodosha Congrats :beer:
Marcello Rocha
@mereghost
Feb 14 2017 16:02 UTC
@jodosha AWESOME! :beers:
Luca Guidi
@jodosha
Feb 14 2017 16:40 UTC
:sparkles:
Dean Gerber
@dean_gerber_twitter
Feb 14 2017 21:51 UTC
Evening. @jodosha congrats on beta 1 😀
Quick question is anyone experiencing empty flash messages after upgrade from 0.9.2 to 1.0.0.beta1? Everything else seems to be working fine after upgrading.
Sean Collins
@cllns
Feb 14 2017 21:54 UTC
sorry to hear you're having trouble with that. thanks for reporting it. this could be the cause? hanami/controller#202
Anton Davydov
@davydovanton
Feb 14 2017 23:59 UTC

folks, I'm trying to update hanami to 1.0 and I have this error:

config/environment.rb:46:in `block in <top (required)>': undefined method `environment' for #<Hanami::Configuration:0x007fe17d7a7810> (NoMethodError)

anyone know how I can fix it? @cllns do you have any ideas?