Dan Tappin

Joined

8,120 Experience
0 Lessons Completed
9 Questions Solved

Activity

Posted in Jekyll as a support page?

Took a run at this - the docs are ok but not 100% clear to the newbie. Steps so far:

  • created a /jekyll/support in my site root to hold the templates etc.
  • run jekyll build -d ../../public/support from my site root
  • the 'Front Matter' section should be noted in the Tutorial - if you forget this once you re-build your Liquid code never gets processed

Pretty good so far.

Posted in Jekyll as a support page?

Can you use Jekyll (https://github.com/jekyll/jekyll) as a embedded help page?

From the showcase section this is pretty much what I am looking for:

https://dev.twitch.tv/docs/ (says built with Jekyll)

I just want to dump all these states files in /support on my main Rails app. I then can manually maintain the help files etc. I want to build a full service one with Helpy but I have given up for now trying to sync the authentication between the two apps. This is way easier to maintain also.

Eliminating the @model.nested_model.joins(:related_model).order(:name) did the trick.

This did the trick for me:

spring: "bin/rails test"

I stripped my Guardfile down to this. If I edit a controller guard starts ALL tests.

guard :minitest, all_on_start: false, all_after_pass: false, spring: true do
watch(%r{^app/controllers/(.+)_controller\.rb$}) { |m| "test/controllers/#{m[1]}_controller_test.rb" }
end

Am I missing something here?

Posted in Testing and Session Variables

I had a rouge conditional wrapped around my code where I set the enrollment. This fails in testing but passes in production. Tweaked that and now it works fine.

Posted in Testing and Session Variables

In my Rails 5 app I am using Pundit for authorization with a custom context. I have a multi-tenant application and I have an Enrollment model that joins my User and Tenant model. This was a single user can have authorization scoped to the tenant. The app works fine - the tests not so much.

Here is some of my code (built by a third party a while back):

class ApplicationPolicy
  attr_reader :enrollment, :record

  def initialize(enrollment, record)
    @enrollment = enrollment
    @record = record
  end
end

In my application controller current_enrollment is set from a session variable.

So here is a typical policy:

  def download?
    return true if enrollment.has_role?('administrator')
    false
  end

Thats a valid fixture but my tests will fail with NoMethodError: undefined method 'has_role?' for nil:NilClass. Clearly the enrollment in the policy is not set. Done lots of searching on this but could not find another similar question etc.

It's not a Pundit issue - the session variables seem to not be set in test environment.

Posted in Best Rails error monitoring software ?

Sentry.io is pretty awesome.

Posted in User 'folders' for uploaded files?

Is there any best practice / tutorial for organizing user uploaded files?

In my app I have an 'uploadable' polymorphic structure where the user can upload files to pretty much any model etc.

Currently the structure is flat - all files ar dumped into a bucket per associated record. My users are going to want to organize these in a normal MacOS Finder / Windows Explorer file structure.

My thought is to add a nested 'folder' structure DB table (with the ansectry gem). It belongs_to the uploadable record and then each upload belongs_to a folder. If there is no folder it's just a root file or a have a default root folder for consistancy.

Does this seem reasonable? Am I missing anyting here?

I have a Rails app with nested attributes. My MVC works great except when the nested validation fails. Here is my parent model controller:

    def update

        if @model.update(model_params)

            respond_to do |format|
                format.html { redirect_to @model, notice: 'Model was successfully updated.'}
            end

        else
            render :edit
        end
    end

The issue is then in my model view when I build the nested form again:

<%= f.simple_fields_for :nested_model, @model.nested_model.joins(:related_model).order(:name)  do |i| %>

I get all the original nested_model items in the form but the new ones that were passed in the params are not added. This works 100% when there are no validation issues. How do I add those invalid nested_model items back in here. I feel like there is an existing solution to this that I can't seem to find easily.

I stumbled on half of the solution - if I put my controller template in inherited_resources_controller that seems to fix the controller template issue.

After getting tired of rebuilding all my default scaffold models, views and controllers I decided to create custom templates as outlines on various blogs / SA questions etc.

I created:

/lib/templates/active_record/model.rb
/lib/templates/rails/scaffold_controller/controller.rb
/lib/templates/erb/scaffold/_form.html.erb.tt (and so on for the actions etc.)

I added this to my config/application.rb file:

config.generators do |g|
      g.orm             :active_record
      g.template_engine :erb
      g.test_framework  :test_unit, fixture: true
      g.skip_routes  true
end

I then run the scaffold:

rails g scaffold MyModel name description notes:text flag_active:boolean slug company_id:integer
Running via Spring preloader in process 52960
      invoke  active_record
      create    db/migrate/20200130165523_create_my_models.rb
      create    app/models/my_model.rb
      invoke    test_unit
      create      test/models/my_model_test.rb
      create      test/fixtures/my_models.yml
      invoke  resource_route
       route    resources :my_models
      invoke  inherited_resources_controller
      create    app/controllers/my_models_controller.rb
      invoke    erb
      create      app/views/my_models
      create      app/views/my_models/index.html.erb
      create      app/views/my_models/edit.html.erb
      create      app/views/my_models/show.html.erb
      create      app/views/my_models/new.html.erb
      create      app/views/my_models/_form.html.erb
      invoke    test_unit
      create      test/controllers/my_models_controller_test.rb
      create      test/system/my_models_test.rb
      invoke    helper
      create      app/helpers/my_models_helper.rb
      invoke      test_unit
      invoke    jbuilder
      create      app/views/my_models/index.json.jbuilder
      create      app/views/my_models/show.json.jbuilder
      create      app/views/my_models/_my_model.json.jbuilder
      invoke  assets
      invoke    coffee
      create      app/assets/javascripts/my_models.coffee
      invoke    scss
      create      app/assets/stylesheets/my_models.scss

The issues are that the model and controller templates are not used and the views work fine.

I found this post https://stackoverflow.com/questions/23624311/scaffolding-rails4-empty-controller detailing that active admin (which I use) uses inherited_resources_controller. I am guessing that is part of the issue but I am at a loss how to fix this. I can't seem to find an obvious solution posted anywhere else.

UPDATE

https://stackoverflow.com/a/20906765/1135515

The model template needs to be in:

/lib/templates/active_record/model/model.rb
And the controller template needs to be:

/lib/templates/rails/inherited_resources_controller/controller.rb
My updated question would be then why is rails using inherited_resources_controller vs scaffold controller and can I fix that?

👍

I will say that ElasticSearch / SearchKick is amazing. Glad to have finally purged all my old Ransack code.

Hmmm... now just read it doesnt support server side processing yet :(

Posted in Looking for a secure place to store scanned tax forms

I am pretty sure that AWS S3 has an option to store files with encryption.

Posted in EleasticSearch and Indexing Empty Models

That was the trick - I should have spent more time searching the issues and I would have found that. If any one else hits this issue also note the requirement to add the second level of { } to the order query. That is not obvious. Just spent 10 minutes baning my head against the wall. 🤦‍♂️

Posted in EleasticSearch and Indexing Empty Models

I have added Elastic Search to my Rails app via the Searchkick gem. I have hit a snag - when I have a model with no records I get these errors:

Searchkick::InvalidQueryError: [400] {"error":{"root_cause":[{"type":"query_shard_exception","reason":"No mapping found for [name] in order to sort on"...

My controller by default is trying to sort the results but there are no results in the index so this fails. A plain old .search("*") returns no results - as expected. It seems that the reindex runs but the index doesn't build the mappings because there is no data.

I know I could add some logic before my search (like a Model.count) but this seems non-ideal. Am I missing a basic ElasticSearch feature here or any other ways to avoid this?

Posted in JSON Web Token Authentication From Scratch Discussion

i am looking at adding an API to my app i want to keep it simple like many other apps uses and a single API key for the account to access my account data.

All these tutorials seemed geared towards user based authentication.

is one way better or worse than the other?

Posted in Bizarre cache issue

How do I force the browser to always refresh parts or all of the content?

I have a bizzare issue where the browser is caching parts of the site. For example I have a 'History' for a model row in the DB. I add a comment, update etc. If I visit the History page then navigate to another action that adds a history then return to the history page it is the old version. If I refresh I get the new content. I am now finding these issues everywhere in my site.

I can confirm that there are no hits to the server so it's not a Rails caching issue - seems to be browser based.

Any ideas?

logo Created with Sketch.

Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

© 2020 GoRails, LLC. All rights reserved.