Dan Tappin

Joined

8,820 Experience
5 Lessons Completed
9 Questions Solved

Activity

Posted in Datatables From Scratch Using Hotwire Discussion

I felt the same way at first then realized (as Chris mentioned) the table html is very restrictive. I have div tables that look like tables on the desktop and then switch to a 'card' like view on mobile. You could never do that with a standard table. For example my tr class is:

grid grid-cols-3 block border border-gray-200 md:border-0 md:table-row md:table-row p-3 md:p-10 space-y-0 md:space-y-3 hover:bg-gray-100 bg-white rounded

BY default on mobile I get a business card looking record then the md breakpoints switch to a div table on larger screens.

Did you import it?:

import { Dropdown, Modal, Tabs, Popover, Toggle, Slideover, Alert } from "tailwindcss-stimulus-components"
application.register('alert', Alert)

Posted in Datatables From Scratch Using Hotwire Discussion

Hmmm - very weird. I managed to add a second turbo frame like this: data: { turbo_frame: "employees employees_index", turbo_action: "advance" }. I suspect I was thinking this was a stimulus target etc. Removed the second frame and it's back to normal. Interesting that turbo still worked.

Posted in Datatables From Scratch Using Hotwire Discussion

Not sure what changed - as I type turbo is updating the results and then the search field loses focus as I type and I lose the odd key stroke.

Posted in Datatables From Scratch Using Hotwire Discussion

How do you get Pagy to update the URL bar and history etc? I tried applying link_extra: 'data-turbo-frame="employees" data-turbo-action="advance"' to pagy but that doesn't work.

I have a multi-tenant app with various models. I want to start tracking data point for future stats and business intelligence integrations etc. Think of this like Amazon and you wanted to track each day the number of active users, categories, users, sales etc.

Is there a gem / framework out there that is used?

I thought of just writing one from scratch - as simple as a has_many type relationship where there is only a type column to match the given model and then a json or hash column to store the data points. This way I can be flexible and adjust on the fly.

I just realized as I type this I can have two kinds of data points: one for a generic model and one on the record level:

User
stats_id: 1
stats_type: User
data: { comment_count: 45, some_nested_data: { nested_data: 56, second_nested_data: 'abc' } }
date: 2021-02-09

Users
stats_id: nil
stats_type: User
data: { user_count: 45, some_other_nested_data: { nested_data: 56, second_nested_data: 'abc' } }
date: 2021-02-09

I think it's simple to just create a single Stats model and the nil id's just hang as raw data points that I can access with a custom SQL call on the model level.

I would run a rake take each morning to populate these for each model then for each record as required.

Does any one see a big issue here or have suggestions / feedback?

Posted in Offline Functionality

Are there any resources / demos / tutorials etc. that cover offline mode for app? i.e. your app still needs to work where there is no internet and then sync back to the host when you connect.

Any specific Rails issues on these?

Posted in Can I authenticate the static files served with Rails

I have a static Jekyll site on my main Rails app in /public/support/. I would like to protect this site and only allow authenticated users to access it. Can I do that?

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?