How to Use Present? in Ruby Projects

How to use present? in Ruby projects?

You don't have to run Rails to use

require 'active_support/all'

or if you just want a specific extension, like blank? for string then just

require 'active_support/core_ext/string'

I looked up present? and it's actually defined as

def present?
!blank?
end

blank defined in active support as

def blank?
respond_to?(:empty?) ? !!empty? : !self
end

Ruby's `.present?` without Rails?

Active Support is broken in small pieces so that you can load just what you need. For .blank? and .present? methods it would be enough to require:

require 'active_support/core_ext/object/blank.rb'

As docs say.

Object#nil? , Array#empty? and Hash#empty? already defined so you dont need anything to require to use those.

Make sure active_support gem installed in your system

Rails: present a choose from to fill an attribute

collection_select will help you:

<%= form_for @project do |f| %>
<%= f.collection_select :owner_id, Person.all, :id, :name %>
<%= f.submit %>
<% end %>

How to run a Ruby project

Maybe it's a Ruby on Rails project. Try these lines in the console in the project folder:

gem install bundler
bundle
rake db:create && rake db:migrate
rails s

These commands will start Rails server at localhost:3000. This project requires DB to be installed. (DB type depends on Gemfile content). Also you should check if config/database.yml file is present.

Prevent view to access models

Ok. Here's the nut of it. (This is incomplete code and is meant to indicate direction, I've removed a lot, so you'll have to fill in the blanks.)

First, I create a module called ActsAs::Rendering. This provides an instance of ActionView::Base, which is the key to rendering anywhere.

module ActsAs::Rendering
private

def action_view() @action_view ||= new_action_view end

def new_action_view
av = ActionView::Base.new
av.view_paths = ActionController::Base.view_paths
av.class_eval do
include Rails.application.routes.url_helpers
include ApplicationHelper
end
av
end

def method_missing(meth, *params, &block)
if action_view.respond_to?(meth)
action_view.send(meth, *params, &block)
else
super
end
end

def render_partial(file_ref)
render(partial: "#{file_ref}", locals: {presenter: self})
end
end

Then, I create a PresenterBase that includes ActsAs::Rendering:

def PresenterBase
include ActsAs::Rendering

class << self

def present(args={})
new(args).present
end

end # Class Methods

#==============================================================================================
# Instance Methods
#==============================================================================================

def initialize(args)
@args = args
end

private

end

And now I create a Presenter class that implements present.

def FooPresenter < PresenterBase 

#==============================================================================================
# Instance Methods
#==============================================================================================

def present
render_partial 'path/to/foo/partial'
# or do a lot of other cool stuff.
end

end

And my views all begin with:

- @presenter = local_assigns[:presenter] if local_assigns[:presenter]

And now, the view no longer has access to anything except its presenter.

* NOTE *

There's a bit more, but I have to run out. I'll update later.

Where to store 'concerns' in a Ruby on Rails project? (Rails 5.2+)

the file path that using include or extend Rails does some magic when starting to autoload a lot of things so you don't have to worry later when you call "Bar". This talk is really helpfull to understand WHY you can just do include Bar inside a rails model without much thinking https://www.youtube.com/watch?v=I0a5zv7uBHw

Usually, you want model related concerns inside /app/models/concerns and controller related concerns inside /app/controllers/concerns, but that's just for organization purposes, rails will autoload them even if you use /app/whatever/concerns, so be carefull about name collisions.

You DO need to extend ActiveSupport::Concern if you want to use the syntax sugar that Concerns provide, but at the end they are just modules that can be included. https://api.rubyonrails.org/classes/ActiveSupport/Concern.html check this examples, concerns are just a way to write modules to share behaviour with a more friendly syntax for common rails patterns.

extend is a method of Object https://docs.ruby-lang.org/en/2.6.0/Object.html#method-i-extend
include is a method of Module https://docs.ruby-lang.org/en/2.6.0/Module.html#method-i-include (and Module inherits extend from Object)

Create default project model object after user sign up

So called callbacks will serve you well:

In your model, you add something similar to:

user.rb

# this calls the function defined below after the creation of a user
after_create :default_project

# put callback functions into the private sector, as it will only be used for this model
private

def default_project
Project.create(:user_id => this.id, :name => "Users", ...)
# do more stuff
end


Related Topics



Leave a reply



Submit