Organizing shared layout and assets between several projects - ruby-on-rails

Every time I create a new Rails application, and for the admin part, I copy the layout and assets from another project to the new project.
I think it's obvious how many problems this approach causes. I am thinking about organizing my layout, and assets in a proper way.
What I have in mind is creating a gem and putting everything in that gem. But before I try that, I wonder if there is a better way for organizing these files?

Gem is good if you want to share codes among apps.
If, like your case, the shared parts have routes/views, an engine would be better suited, which is also a kind of gem. You can check the guide for details: http://guides.rubyonrails.org/engines.html

Related

Integrating multiple apps in Rails

I am working on a project in Rails. It contains multiple sub-projects. They have a common login and the rest is separate for all. I want to integrate them all in the main project. The sub-projects are pretty big, so putting them all together will make it cumbersome to manage the code. Is there any way to integrate the sub-apps or gems in the main project?
You might consider breaking your apps into engines. It's a great way to isolate functionality and make code modular. Here's a link to the Rails engine docs: http://guides.rubyonrails.org/engines.html
If you need examples of real-world uses of engines you might consider looking at the code for Spree: https://github.com/spree/spree
With Spree you can add custom functionality by installing or building extensions, which are effectively Rails engines.
If you want to reference local gems/engines, you can point to them in your Gemfile like this:
gem 'mygem', :path => '/path/to/gem'
But make sure that the individual gems within your project don't have a .git folder, or you might run into errors regarding sub-modules.

Testing Rails Project/Application Template

There's every chance I've approached this entirely in the wrong way which might be why I'm struggling to find an answer, as is typically the way with Rails.
For clarity, I'm referring to the -m option of "rails new", eg.
rails new [new-project] -m [this is where my template goes]
I've created a project/application template to speed up the creation of a specific project type, with the view that further projects/products will have their own template also.
The template adds in the relevant gems and does a bit of housework to tidy up the output of the gem installation (moving JS/CSS files to /vendors, for example) as well as copying over a generator that gives the option to expand the project further.
I'm struggling to understand if there is a way to test that the project template is working as expected and copying the correct files.
Have I approached this in the correct manner, and is there a way to test project/application templates?
Edit - Further research is suggesting that an Engine may be a better solution. Is that accurate?
A Rails Engine is a way to package some application or application subset so that it is easy to share with other applications. Basically, if you have some models and controllers that can be shared by multiple applications, you can use an Engine. This sounds a lot like what you described as a project template. Engines can be packaged as gems, gems can automatically behave as Engines, include rake tasks, hook into Rails initialization, and more, so why not take advantage of Other People's Work if you can?

Can an admin template be used in a Ruby on Rails web app?

I have been doing UI research and have come across admin templates at http://themeforest.net/. I was wondering how do you apply these onto a web app built on Rails. These templates look very similar to wordpress themes. Are they that easy to configure? Is it just as simple as setting up a link to the database to make the fields form capture data? I've been looking at this theme.
For admin templates I recommend using Active Admin. It's relatively easy to implement and gives you great admin screens with little effort.
Yes, You can. I'm trying to solve the same problem and so far I have a couple options:
1.) do it by hand, I've done this before, it works but takes a lot of time to truly understand how your theme is put together. First I would recommend using the included themes assets exactly as they are bundled with the theme. Don't assume that just because you have twitter-bootstrap-rails gem that the bootstrap classes in the theme will work. Link the assets statically and slowly extract out the static assets and replace them in the asset pipeline once you know they work.
2.) Use the strategy suggested in the install_theme gem (http://drnicwilliams.com/2009/10/06/install-any-html-themetemplate-into-your-rails-app/) the gem itself is not maintained any longer (i'm not sure about any forks), but the strategy is sound. Extract the core parts of the template into partials.
The short answer is yes, but there is no straight forward way to "import to rails"

Where do you put your engines in your Rails project?

I have been putting them in the toplevel. I am wondering if it is more logical to put them in lib. Is there a general consensus or convention?
vendor/engines is my default choice.
I generally make them gems (by creating them with the bundle gem command) and keep them as separate projects on GitHub. Then I can include them into my application like this:
gem 'forem', :git => "git://github.com/radar/forem"
This way, I can make commits to my application and have them be separate to the commits for my engine.
The reasoning for doing it this way is because the engine may be shared across multiple applications and I'd rather not have to make the same change multiple times.

Rails 3: How can I reuse code in my next project?

I want to reuse some controllers, models, stylesheets, images and views in upcoming projects. How is it possible in Rails 3, to have these be standard components, when I create a new project (rails new $project) ?
Generally you would use the same OO principals used in other languages and applications. You can use version control to keep the files in sync if there isolated. However if they are spread about the project you should look into Rails Engines. However you are going to run into a lot of headaches if your code is non orthogonal.
Extract them into Engine or Gems. Structure stylesheets, images in assets. It's not only possible it in fact the standard solution to do. Read some gems to learn this (Devise, kaminari, to name a few).

Resources