General advice and recommended folder structure - Sinatra - ruby-on-rails

How would you structure a simple Sinatra app?
I'm making it right now and I want the app to have these features:
The "app" is more of an admin dashboard for all the information inside it. Then another app will access the information via REST. I have not created the dashboard yet, just the getting of things from the database
Sessions and authentication (have not implemented this yet)
You can upload pictures, and other the other app can display those pictures
I have created a testing file using RSpec
Reports generation via Prawn
Currently the setup is just this:
app.rb
test_app.rb
because I have literally just the app and testing file. So far I have used Datamapper for the ORM, SQLite for the database. This is my first Ruby/Sinatra project, so any and all advice is welcome - what other libraries should I be using, should I put stuff like config.ru, etc.

Sinatra is not opinionated when it comes to your file structure, you can place files however you like. When I first started I just dropped everything in the top level, but over time reading how people structure their code, reading over the source code of gems I've broken up my code into smaller .rb files that fulfill a specific function and places all of them under /lib, it's a convention carried over from rails perhaps but does not have any of the magic associated with it in rails. If you use scss or coffee script they depend on certain folders to exist, you will discover for yourself over time (and even then you can reconfigure them however you wish) and from this you will figure out what works best for you.
if you write a restful api, check out grape - https://github.com/intridea/grape
you will also find sinatra-contrib to be very useful - https://github.com/sinatra/sinatra-contrib
As for what to do with your config.ru - https://github.com/rack/rack/wiki/%28tutorial%29-rackup-howto

Related

What are the reasons to use erb integration for webpacker in Rails 6?

I have surfed a couple of hours through the web but couldn't find any articles/walkthroughs/comparisons touching erb integration of webpacker. I've found 1 question, unfortunately, the author haven't read docs attentively and the answer was right there, so - no any additional info there.
I have seen plenty of articles about vue and react, but nobody says a word about erb. However, it's quite clear why using react/vue/else similiar, it is not with erb.
The theme is quite vast and I expect a little hate towards me, so I'd ask two related questions (but if you have something to tell more about it - that's appreciated).
As I understand - it's vanilla (plain) js (maybe with a flavour of jQuery) caring just about dom and styling, with all the preprocessing made by rails. If it is so why not just continue using sprockets?
And what are the reasons to choose it instead of some react/vue/else framework?
You may use both : a vanilla JS framework (React, Vue ...) and some erb files. I find it interesting to setup my constant and other configuration variables within a .js.erb file that is generated by my Rails app when building the js app.
Things I like to put in this erb files :
schemas of my api, generated by my serializers
constants, like enum
values to be used in forms
To generalize, you can put anything owned by the backend that will not change at run time
this save you a couple API calls to retrieve this data. However, I tend to stop doing this as your JS app and Rails become tightly coupled and you can't use the sources of your JS app outside the Rails app

How to properly include code in .rb files into Rails architecture?

I am currently coding an application in Ruby that does some simple external API calls to Soundcloud's API.
I have developed a bunch of code inside a single .rb file and want to put this into the rails architecture. This Ruby file has the following classes:
class SoundcloudUser
class SoundcloudQuery
class SoundcloudFollowers
Currently I understand that I can put these classes into seperate .rb files, and then just put them into the /models/ folder which then gives me the ability to call these classes from elsewhere in my rails application (using require/include).
My question is simply, is this the correct way to go about this? I am familiar with rails, but I am new to transferring a Ruby developed project into the rails format. I tried searching best practices for this in the Ruby style guide but I didn't really find anything.
On a side note - I wanted to also create another class that acts as a ?service? wherein in checks my local database if an entry already exists in the database, and if not, then it will query new data. My side-question here would be similar - where would this .rb file for this 'service' live?
I hope I explained my question clearly enough, if not, I am happy to add some clarifications. Thank you for your time!
If in Rails, you can put them in either lib/ or somewhere in the main app directory. For example, you can create app/services and put them inside there, and when you restart the Rails server you should be able to call SoundcloudUser (provided you name them app/services/soundcloud_user.rb.
I always look at the Gitlab source code for this. It's a gigantic Rails app but look at this file: https://github.com/gitlabhq/gitlabhq/blob/master/app/services/gravatar_service.rb. Because it's inside an app/services (any name actually), GravatarService can be called from anywhere in Rails. If you want to have some namespacing, you have to put it in app/services/soundcloud/user.rb or lib/soundcloud/user.rb and name the class Soundcloud::User.
For the class that acts as a service, it seems like it orchestrates the logic of "check if (song?) exists, else scrape. Some people put it in a model class, I'd probably put it in a service class a la the Gitlab source code. Hope I helped.

Locomotive CMS with Rails - location for liquid templates

I'm new to both Ruby on Rails and Locomotive CMS, but I'm just starting to create my first site with them.
I've got the engine running in a full Rails app (I'm going to need to deploy it on our own server later on). But it's just spitting out the 'Template' content defined through the admin interface, without any other template/content around it.
I can 'fix' it by shoving the html for the whole page in through this input field. But that's not right, surely? The Getting Started guide talks of putting the templates in the filesystem, at something like: Pages/index/first page. "All pages are inherited from index". I have an index.liquid under views/pages but it's not picking that up... (I've tried a couple of other locations too).
I'm sure this is a dumb question, but please could someone tell me where to put my template in the file system? Or how to point Locomotive to pick it up from the right place?
(I did get the file system liquid template working by defining it through the Rails way, with a route, a controller and adding a liquid template initializer I found here. But then it's missing the variables that should come from the CMS content).
I'm loading the site using bundle exec unicorn_rails. And I'm using Rails v3.2.13, Ruby v1.9.3 and Locomotive_cms v2.2.2.
Thanks!
I'm Didier from LocomotiveCMS.
LocomotiveCMS is a little bit different from the other CMS, in a sense, we offer a tool named Wagon to manage your site locally without having to install mongodb, rails and some other components.
Another huge benefit is that you can write your templates in HAML and your CSS in SASS/ SCSS or Less (we embedded Compass as well) and with our preferred texts editor (editing a whole site in a browser is a nightmare).
That's a nice eco-system in order to be super efficient when it comes to develop a LocomotiveCMS site.
Once you're done with your local work, you can deploy your site to a remote LocomotiveCMS engine in a similar way you push your application to Heroku. Actually, pushing a site will create the back-office for the final end user.
I suggest you to read that page.
http://doc.locomotivecms.com/guides/get-started/requirements
and this one too
http://www.locomotivecms.com/tour
Our message is still not clear on our official website but believe me, we are working to make it better.
Hope it will help you !
Didier

Rails Spree internals

I am trying to use Spree with my RoR application. Ok, I do follow all those guides and FAQs on official website when I want to customize something. That's ok and no problem with it. One question, to which I could not find a clue -- how is that possible, that there is nothing in apps/view, apps/models folders, but it's still working? I mean, yes, I can create something in these folders and redefine the behavior of my views (actually, this is one of the ways of customization), but I really want to understand the internals. I am pretty new to Rails and got used to classic app folder structure.
what you are wondering about is the magic of Rails Engines.
Ruby on Rails allows you to define Engines (your app is one too) and when it looks for views/controllers/etc.. all mounted engines are part of the search path.
So the view is inside the Spree gem, not visible to you - but it still looks in there.
If you put something in your view folder with the same name, it will take precedence over the views in the Rails engine you have in the Gem.
Here is a good guide on how Engines work in Rails:
http://edgeguides.rubyonrails.org/engines.html
One good example of these Engines is the jQuery-rails Gem you probably use inside your Application.
It has no code at all (except for some fallbacks for Rails 3.0 and below that don't have an asset pipeline), but the jQuery.js file in the app/assets/javascripts folder. And since the engine is in the load path you can require the asset that's in there..
The engine itself has the same folder structure as your app (app/views, app/controllers ...)
You can look at the internal structure of Spree here: https://github.com/spree/spree/tree/master/core/app

Why is there Rails.rb files all over the place?

Was digging around my Rails applications and noticed that there are rails.rb files all over the place. In my ruby gems directories like:
...gems\devise-2.0.4\lib\devise\rails.rb
...gems\cucumber-rails-1.3.0\lib\cucumber\rails.rb
...gems\railties-3.2.3\lib\rails.rb
I am assuming that there are executed whenever you issue some command like "rails xxx". So all these extra rails.rb files combine with the original rails.rb file to essentially make one big rails.rb file. Essentially, when we type in "rails xxx" it goes thru all them all?
Just looking for some confirmation PLUS a little more knowledge about this. Thanks.
The best way to understand what these rails.rb files are doing, is to read the source code.
ralties
devise
cucumber-rails
As you can see, in any library the file assumes a different scope. The common behaviour is that the file rails.rb normally contains the code required to initialize the library when loaded from a Rails project.
BTW, this has nothing to do with the script/rails command and there is no "big rails.rb" file.
The files are not generated but are simply source files of these libraries you are using.
In this case they are probably rails-related classes that either extend Rails in some way or modify it or make the library interact with Rails.
Rails is a very common framework in Ruby land so most if not all libraries will have some sort of integration with Rails.
By no means are all of those loaded when you run rails XXX but rather when your application loads these libraries their rails.rb files may be executed to provide some sort of integration with Rails.

Resources