shakycode

Joined

5,400 Experience
2 Lessons Completed
3 Questions Solved

Activity

Posted in Idea for TimeClock Need Advice

I think a feature branch will do the trick. I'm just going to try my best to stay out of master so it doesn't get too far ahead of the branch otherwise it's going to be "merge fun". I may prototype in a separate app just for fun but it definitely makes more sense to do a feature branch. (thumbs up)

Posted in Problem with upgrading to Rails 4.2.2

Btw, if you add the following to your Gemfile it should take care of installing the latest version for you.

gem 'rails', "~> 4.2.1"

Posted in Problem with upgrading to Rails 4.2.2

Sweet! I love when problems turn out not to be problems after all. Happy hacking!

Posted in Problem with upgrading to Rails 4.2.2

This was the last release note from the core team. Unless you're on a branch that hasn't been put into production yet. Rails Release

Posted in Problem with upgrading to Rails 4.2.2

The chmod should work. Out of curiosity were you trying to install the Rails gem as sudo or just as yourself? Sometimes these things matter although you should be able to install Rails in userspace without having to use sudo for global installation.

I haven't touched 4.2.2 is that even a release yet? I thought 4.2.1 was the latest version as of 3/19/15 per Tenderlove.

Posted in Must Read / Must Watch

Besides GoRails I have to suggest Destroy All Software. Especially the season with the Sucks/Rocks app. Incredible refactoring and TDD. Material is a bit dated, but the fundamentals are there. Definitely worth the asking price.

Posted in Active Record Associations

I agree with Chris. I ran into similar use cases using HABTM but ended up using has many through as an alternative. It was a much cleaner option for me.

Posted in Ideas for building a Wiki in my app

Jay, this looks pretty darn solid actually. There are a few places where you can DRY it up, but those can be handled later in a simple refactor. I noticed you're using conditionals in your views. I'm not a huge fan of conditionals directly in the view but in your case it may not be easy to avoid it. You could always extract into a class method, presenter, or a helper if need be. I had to do something similar and Chris helped me get on the right path with abstraction.

Are you open-sourcing your app or is it private? I'd love to take a look at your codebase if you're open to it. I love looking at other dev's design patterns and learning from them, no matter what the skill level. :)

Posted in Idea for TimeClock Need Advice

Hey Chris,

I really appreciate your review and critiques. The refactor makes a lot of sense and is way cleaner and I like the idea of caching the the variable as that will definitely take some hits from the db and get rid of them. I'm still stubbing this all out, so I'll take your suggestions and refactor the models and the controller and post up either a gist or code snippets today to see what you think. I'm hoping to have the basic functionality working in the next day or so. This was more of a weekend project so I didn't really have time to start building, just writing stubs and seeing how it might work.

Would you suggest I work off another branch and test all this or simply write a new tiny app to test the functionality then port it over into my main legacy app? I'm doing my best not to work off of master in case something needs to be pushed quickly.

Posted in Idea for TimeClock Need Advice

What about something like this?

class ClockEvent

  belongs_to :user

  # scope the last_clock_event for a user
  scope :last_clock_event, -> { where("clock_out NULL").last }

#assure the clockevent is completed
  def completed?
    clock_in.present? && clock_out.present?
  end

end

class User

  has_many :clock_events

#Method to check if last_clock_event is completed and if so instantiate a new event or if not load the last_clock_event
  def current_clock_event
    ce = clock_events.last_clock_event
    ce.completed? ? ClockEvent.new : ce
  end

end

class ClockEventsController < ApplicationController

#Instantiate either last_clock_event or ClockEvent.new to be passed into the form
  def index
    @clock_event = current_user.current_clock_event
    render :index
  end

end

Note, this is a work in progress and I'm looking for peer review.

Posted in Idea for TimeClock Need Advice

Update, too early to be doing this but my scope was botched, this should fetch the last record with clock_out: nil

scope :current_clock_event, where(clock_out: nil).last

Still need to figure out a conditional of current_clock_event or setting up a ClockEvent.new depending on if the current_clock_event exists or not.

Posted in Idea for TimeClock Need Advice

Ok, so I'm stubbing through this and trying to make sense of things. So here is the scope I'm looking to build for current_clock_event

```class TimeEvent
belongs_to :user
scope :current_clock_event, last.where(clock_out: nil)
end

Doing this I'm trying to fetch the last record for the user where `clock_out` is nil.  The problem is AR won't let me pull the last record and chain a where clause to the `.last` method.  

What would be the best way to fetch/scope the record looking for the last record where `clock_out: nil`?

Also in the controller, I'm trying to figure out the cleanest way to do the following.
``` class ClockEvents
def index
  @clock_event = current_user.current_clock_event #grab scope from model or create new ClockEvent?  Need guidance on this.
end
end

So basically what I want to do in the controller is to fetch the current_clock_event if it exists (such as last record for the user with a clock_out of nil) and if the ClockEvent is complete instantiate a new event to be passed into the view/form. What would be the best way to do this in the controller to ensure that I'm either completing the last ClockEvent or if completed to a ClockEven.new?

I really shouldn't have nuked my repo that had this code 2 years ago. I had this all figured out a while back but nuked it since I didn't think I'd ever need it again.

Thanks in advance and let me know if you need more detail. I'm open to whatever design patterns you have in mind, just looking to make it clean and work out of the gate. I'll get to model validations and call backs in a bit.

Posted in Idea for TimeClock Need Advice

I think having it available in both the controller and model will be necessary. I'll work on getting that together, shouldn't be an issue. I like the idea of caching the total_hours value. This is good stuff. :) I'll start stubbing all this out and will post up questions if I have any.

Cheers!

Posted in Idea for TimeClock Need Advice

I agree that a ClockEvent should have a clock_in and clock_out value. And yes, you're right it's easy to calculate the time between two date objects especially with to_i then parsing the seconds into a readable time which I've already written a time parser for (HH:MM). One thing I think I should look at is using .round in the calculation to deal with the seconds as we pay off of hours/minutes worked so I'll probably want to round to shred the fragments. No one gets paid for 15 seconds :)

I think doing something like current_clock_event makes a lot of sense and by scoping it will ensure I can complete the time clock cycle of clocking in/out. Does it make more sense to architect that in the controller or the model? On the ClockEvent controller the index could be something like @clockevent = current_user.current_clock_event ||= ClockEvent.new.

The forgotten clock in/clock out is easy to do by writing a rake task looking for time ClockEvent every week and notifying HR/Payroll via a mailer of the entries so he/she can go back in through a view/controller and make simple crud updates.

My only question that I'm a bit fuzzy on is calculating the total number of hours between two given dates. Calculating the difference in hours/sum for each day is easy but I'm having a brainfog moment with calculating the sum of total number of hours between date ranges. Maybe you can tip the hat on how to do that?

I was thinking to calculate the hours I'd actually store it as a value in the current_clock_event. Through a callback or something. So like an after_save model call back which will calculate the diff in integer, convert it to a string, and post it in a field in the ClockEvent called total_hours. This can be updated when the HR person goes back in and edits/makes corrections.

In reality after reading your post and thinking about it most of the day this should be pretty simple. Just haven't worked with this sort of thing in a while so I needed to think it through and get some feedback from my favorite community <3

Posted in Non Restful actions in the controller

This is good work, especially for a first pass. I'd also try to avoid the case statement in your controller. I mean if it works, then great. But I usually like to put more lengthier logic in the model whenever possible and create a class method. But that's just me.

Posted in API Design Decisions

Been reading up on this thread which is quite interesting. I'm also not a fan of putting the token in the url. It introduces potential security issues especially on a network that might get hit with Man in the middle attacks. All they need to do is sniff the auth token and boom it's done. I have a lot of experience with HIPAA and PHI so your assets that are PHI is something you'll definitely want to secure. But to serve up the assets faster you couch "technically" store them on a CDN that meets HIPAA requirements. That's something I had to do in one of my apps for large imaging/MRI records.

Posted in Ideas for building a Wiki in my app

Jay,

Congrats on getting it working. I'm looking to implement similar functionality. Do you mind sharing what you came up with?

Posted in Idea for TimeClock Need Advice

I'm wanting to integrate Time Clock functionality into an existing Rails 3.2.21 app. I think I have an idea of how to do this, but I need some advice on the how to handle different scenarios.

So here's my thoughts on the models:

User
has_many :clock_events
ClockEvent
belongs_to :user
attr_accessible :clock_in, :clock_out, :explanation

The basic functionality in the controller and view, will let current_user clock in and clock out with an explanation in case they are early/late/didn't clock in by accident. I also need to figure out how to sum the clock_in and clock_out times/dates for each shift and then for a date range.

What I'm getting tripped up on is when a user logs in to clock in, the thought is to create a new clock_event and allow them to either clock_in or clock_out. Then perform some sort of validation that checks if they are clocking in and the last even was not a clock out, to give them an error and prevent them from skewing the clock_in/clock_out process. I'm pretty sure I can do some custom class method validations to make this work. But ultimately I'd like each time entry to have a clock_in and clock_out instead of iterating over a new instance every time they go to clock in/out. So basically if I am clocked out and go to clock in a new object will be created that allows me to clock in. I'm just trying to think of the best way to work with that current clock_event to ensure that both clock_in and clock_out are populated before allowing a new object to be created.

This is all a 30,000 FT view of the idea, but I could use some advice on how to build something simple that keeps track of clock_in/clock_out, allows me to sum/calculate the time between clock_in/clock_out, performs sum/calculation for all completed clock_event between a given date range.

It's early and I'm scatter brained, so I'm sure I'll need to flesh this out some more and start writing some sample code. But if anyone has any ideas on how to do this cleanly I'd appreciate the help. I wrote something like this 3 years ago when I was learning Rails but lost the repo so I totally forgot how I managed to do it back then. lol

Posted in How to make a good Sidekiq job?

I think Chris' advice is sound and would really help prevent duplicate job runs. I'm doing something similar now where I mark the record as an email being sent and in the Sidekiq perform method include a check against this field.

Also, just a protip. With Sidekiq or any other background job gem, less is more. So the less it does the better. Also make sure you're passing simple objects as arguments to your worker methods such as user.id instead of @user. Let SideKiq do the heavy lifting on the backend when the job executes instead of passing a loaded object to the worker.

Posted in Advice on building a 'reports' feature

And after reading the OP's post again, I really think you need to abstract as much as possible and separate concerns and responsibilities. Otherwise this could quickly turn into a "God Object" scenario. I'm dealing with Zeus on my side. lol

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.