Creating a gem for cells components - ruby-on-rails

I want to create UI components to enable users to edit data present in a specified structure. I decided to give Cells a try, but I need to encapsulate the whole code in a gem, since I want to provide this components as an extension to an existing gem (criteria_operator), which is the source of the classes used for the data.
Is this possible? And if yes, how do I start? Normally, cells expects you to create views and models in specific folders of the default rails folder structure. When creating a gem, I only have the lib folder...
If you need any additional information, please just point it out in the comments. I wasn't sure if there is anything useful I could provide.
Disclaimer: The gem mentioned was created by me, and this question isn't intended to promote it.

To use another gem- criteria_operator, just add it to your gemspec:
s.add_runtime_dependency('gem_name', '~> <version>')
or add it to your gems's Gemfile.
To have models and views in your gem, you can use rails engine.
Rails engine allow you to wrap a specific Rails application or subset of it inside a gem.

Related

Include a gem's code as part of a rails project

My rails 4 project depends on Comfortable Mexican Sofa (CRM) which is loaded as a gem.
I checked out this gem as a submodule of my git repository.
Moving forward, this gem is becoming part of my project: I'm adding features to it
that depends on classes of my projects.
I would like to keep this gem as a submodule (to be able to pull changes from
the main repository) but still be able to add code that rely on my project.
What's the cleanest way to achieve that ?
(should I put the whole gem submodule into my lib folder for instance ?
should I keep it referenced as a gem ? etc...)
should I keep it referenced as a gem
Yes, you need to keep it referenced as a gem.
One of the benefits of using external libraries is that they are maintained by a group of very clever people (most of the time). It would be highly unlikely that you're going to be able to keep the submodules you include up to date as much as the gem owners etc.
What you're best doing is either overriding specific parts of the gem (EG like how many people override Devise functionality with their own controllers), or to see about creating an API for the gem, which you can populate from your app.
For me, in development, separation is always better then binding. I would recommend to leave it as gem and develop in the separate repository.

Overriding the default views in RailsAdmin

I am using Ruby on Rails 4.2.1 with RailsAdmin. The gem works excellent, but I have the requirement that the layout of the admin panel and the forms must look different than what is generated by default. For example, the navigation should be horizontal top, the forms should order the fields in two columns.
So far I haven't find a way to copy the views locally and modify them (like in Devise for example). I have tried to replicate the views manually in the respective path under my views folder by copying the original views, but I got problems with the helper methods that are part of RailsAdmin not being accessible from my views.
I dug deeper and found that there is a task copy_views, it was referred to in questions for the older versions of the gem, but if I try to use it now rake rails_admin:copy_views, it is not available anymore.
Am I doing something wrong, or is there another way to do this?
You can create folders in your app
app/views/rails_admin/main for https://github.com/sferik/rails_admin/tree/master/app/views/rails_admin/main
app/views/layouts/rails_admin/ for
https://github.com/sferik/rails_admin/tree/master/app/views/layouts/rails_admin
Put modified files there. It can get a little messy and you will need to update the files if the gem changes.

Rails gem with CSS and javascript

I've got a simple rails gem (created using bundler) and I'd like to extend it by adding some CSS and javascript functionality. However, I'm unsure how to go about this and where to add the files. In particular, I need need more information on how it all fits together with the asset pipeline once it gets included in another project.
Can anyone give me the lowdown on how this works and either provide some simple examples or link to a tutorial? Literally 1 css and 1 js file is all I'm looking to include. Thanks.
You could write the gem as an engine. This allows you to have an app folder in the gem just as any Rails application would have. You can add models, views, controllers, assets etc.
Once you have it set up it's quite intuitive and it's a familiar way to create a gem if you're used to creating Rails apps.
This should get you started:
http://coding.smashingmagazine.com/2011/06/23/a-guide-to-starting-your-own-rails-engine-gem/

Best practices when including Rails models in another application

I'm developing a ruby application that uses the models and data from another Ruby on Rails web application as its main data source.
The Rails models were included in this application by including the environment.rb file in the main file like this:
# Require Rails
require_relative "../../RailsApp/config/environment.rb"
This works but there are uninitialized dependencies when loading models that use gems that are defined in the Rails Gemfile. (For example, acts_as_taggable_on, rack-pjax, devise, etc)
This ruby application dependencies are also managed through Bundler, so at the moment the only way to get the application working is to copy and paste the contents from the Rails' Gemfile into the ruby app's Gemfile.
Obviously this approach is not optimal as the gem requirements are duplicated.
Is there a better way to include Rails and the dependencies that its models require in another application? Is there a way to include a Gemfile into another?
Here are some options, in order of simplicity
Just keep everything in one app, a lot of stuff is easier this way
Use plugins to share common code
Use web services to share data
You could extract the models and code out from RailsAppA into a Gem. RailsAppA then includes that Gem and uses it.
The gem can remain in a private repository and does not need published.
Both apps would then do something like:
gem "yourapp-modelage", git: "http://github.com/you/yourapp-modelage.git"
Then, App2 would also use that Gem... How much goes into the Gem will depends on how much you need to re-use.

Ruby On Rails CMS Framework

I want to create a framework for rails application. It will be a rails application but packed into gem (like a Radiant CMS).
It must work like this:
gem install cmsframework
and then:
cmsframework the_app
After that we have a skeleton framework for a rails app, without any controllers, etc. All controllers are loaded from cmsframework gem.
If I want to rewrite some files (for example public/styles.css), I must simply create it in my app (the_app).
If I want new functions in my app I can create a plugin. But the main functionalities must be loaded from cmsframework gem.
What is the best way to implement this?
Maybe start here: http://guides.rails.info/plugins.html. Pay close attention to the parts about adding custom generators and packaging as a gem. This may help as well: http://railscasts.com/episodes/218-making-generators-in-rails-3.
You can use this framework it is very good CamaleonCMS

Resources