jeudi 13 août 2015

Ruby on Rails multiple classes with same name under different namespace

So I have some classes in Rails that do a bit of funky stuff. I have some classes under a Rails application named ApplicationName. They are autoloaded as part of Rails on the boot of my application server. Some are ActiveRecord models and some are just PORO models that are created to deal with some other stuff and some are service classes which are also PORO's. Some of these classes have the same name but are under different namespaces such as User and NameSpaced::User which represent different objects with similar concepts. In one of the namespaced classes I do some ETL work to get a foreign object to meld into an ActiveRecord model in my database. Since the ActiveRecord model by default is under the global ApplicationName namespace I figured ApplicationName::ModelName would work and I would be returned the top level object (the ActiveRecord model) I expected. Instead I got an unintialized constant error. If I do a ApplicationName::Application::ModelName I am able to return it but I get a warning about the class referencing the toplevel namespace (as I expected considering the ActiveRecord object resides up there). The ModelName model conflicts with another model under a different namespace (for the sake of argument we'll call it DifferentNameSpace). All in all things look like:

module ApplicationName
  class Application < Rails::Application
   # do autoload stuff here
  end
end

class ModelName
end

module DifferentNameSpace
  class ModelName
  end
end

Is there any way to specifically call ApplicationName::ModelName or to do an ApplicationName::Application::ModelName without the warning? Right now it works if I do ::ModelName but that looks so...ugly.



via Chebli Mohamed

Rails collection_check_boxes - wrap each checkbox with

Brand new to Rails. How do I add a <li> wrapper to each checkbox / label element generated by the following code?

<%= f.collection_check_boxes :publish_to, [['YouTube'], ['Hulu'], ['Roku'], ['Owned Website'], ['Other']], :first, :first %>

The final outputted HTML would look like:

<li class="checkbox-wrap">
  <label></label>
  <input type="checkbox"/>
</li>

Thanks in advance!



via Chebli Mohamed

Paperclip files get deleted after each deploy

I use the Paperclip gem to store pictures, and on localhost it works perfectly. However, any pictures I add to my live app get deleted after every deploy.

I use Git to deploy. Here's my deployment process:

$ bundle exec cap production deploy
$ ssh root@xx.xxx.xx.xxx
$ chmod -R 777 /rails_apps/app/releases
$ cd /rails_apps/app/current
$ cp config/database.yml.sample config/database.yml
$ RAILS_ENV=production bundle exec rake assets:precompile
$ /etc/init.d/apache2 restart

Has anyone else run into something like this?


UPDATE:

This is not a duplicate, because the answer to this question, which is to add this line to my deploy.rb:

set :linked_dirs, fetch(:linked_dirs, []).push('public/system')

causes Paperclip to break entirely. Previously I had had an issue with not having permission to add images with Paperclip, resulting in this error:

Errno::EACCES in UsersController#update
Permission denied - /rails_apps/website/releases/20150807211111/public/system/users/avatars/000/000/562

But running this command on my server fixes the permissions:

chmod -R 777 /rails_apps/website/releases

However, modifying my deploy.rb file as shown above, causes the chmod -R 777 command to no longer work, and I once again don't have permission to add images, resulting in the same "Permission denied" error.

So that question does not supply a valid solution to my problem.



via Chebli Mohamed

Ruby on Rails - redirect_to the next video that is not marked as completed

How can I redirect to the next lesson that does not have userLesson (problem is lessons belongs to a course through a chapter)

Models:

class Course
    has_many :lessons, through: :chapters
end

class Lesson
 belongs_to :chapter
 has_one :lecture, through: :chapter
end

class User
  has_many :user_lessons
end

class UserLesson
  #fields: user_id, lesson_id, completed(boolean)  
  belongs_to :user
  belongs_to :lesson
end

class Chapter 
  has_many :lessons
  belongs_to :lecture
end 

here user_lessons_controller:

class UserLessonsController < ApplicationController
  before_filter :set_user_and_lesson
  def create
    @user_lesson = UserLession.create(user_id: @user.id, lession_id: @lesson.id, completed: true)
    if @user_lesson.save
      # redirect_to appropriate location
    else
      # take the appropriate action
    end
  end
 end

I want to redirect_to the next lesson that has not the UserLesson when saved. I have no idea how to do it as it belongs_to a chapter. Please help! Could you please help me with the query to write...



via Chebli Mohamed

rails 4 best inverse_of best practices?

I am developing a web app using rails 4 for the first time. I am making all of my model associations bidirectional and using inverse_of wherever it is allowed.

From reading the documentation, I've developed the impression that this is probably the best practice, but that's never really spelled out clearly anywhere.

I'd appreciate any general advice in this regard from experienced rails developers. I hope the question is not too vague to have value here.

Thanks!

Update: In addition to non-standard names, there appear to be two main additional cases where explicitly setting inverse_of is needed:

  1. for invalid_automatic_inverse_options ( http://ift.tt/1hAovkA )
  2. if you're accepting nested attributes as per ( http://ift.tt/1qoax2y )


via Chebli Mohamed

Ember.js input focus lost on valueBinding

I'm creating application with ember.js + Rails

Here is my templates/application.emblem:

#wrapper
  article.new
    = view Ember.TextField valueBinding='newPostName'

the problem is, that after clicking on input, and pressing any key, focus on that input is lost.

It happens only first time and I wonder why. After clicking again, I can type any string and everything works fine.

Here is my Gemfile:

gem 'ember-rails'
gem 'ember-source', '~> 1.13.5'
gem 'ember-emblem-template'
gem 'emblem-source'



via Chebli Mohamed

How to break Rails application and return 404 as JSON

Rails application working as API service. I use mongoid

In a controller i write now:

class UsersController < ApplicationController
  def show
    @user = User.find params[:user_id]
    unless @user
      render json: { error: I18n.t('user.messages.not_found') }, status: :not_found
      return
    end
    # ... some actions with @user object 
  end
end

I need make code cleaner and move this code in module. I assume that the code in controller may look like this:

class UsersController < ApplicationController
  def show
    @user = User.find params[:user_id]
    not_found? @user

    # ... some actions with @user object
  end
end

or like this

class UsersController < ApplicationController
  def show
    @user = found? User, params[:user_id]

    # ... some actions with @user object
  end
end

One of example how it can work is Pundit gem (http://ift.tt/1cmAaug). In my controller i write:

...
def index
  authorize User, :index?
  @users = User.all
end
...

if user hasn't access to this page then Pundit raise exception and return to client error message with 401 http code.

What is better way to refactor my code to make controllers cleaner?



via Chebli Mohamed