What is the best way to extent and contribute to a rails gem/engine
I have found this blog gem/engine , which I want to use it in my rails3 application.
But there are few modifications / features I would like to have and I'm willing do code them. And after that I would like to add them to the original gem/engine (if the author permits)
But I'm confused with how and where to code and test my new changes.
So far I have done the followings
1 - Fork the gem/engine to my github account
2 - clone the source to my local machine
3 - created a sample rails app and added the gem (from my github account)
My question is,
How can I do the code changes to the gem and test them. Gem itself has used rspec and I could do that too, but some of the changes I'm planning (like layout changes), is litttle hard to check with rspec.
this gem in using rails > 3
There is an excellent ScreenCast about it: RailsCasts: Contributing to Open Source
Related
Working with user resources is an essential part of every application an so there it's a task which should be automated as much as possible.
As for Ember I found a tutorial https://github.com/heartsentwined/ember-auth-rails-demo/wiki
which describes how it can communicate with devise-based authentication system. And, wow, it's a hell of a boilerplate:)
Have something changed with new devise for Rails4 or things are the same?
ember-auth dev here.
Edit / Update for googlers: I have now built a rails 4 app, with devise, ember, and ember-auth. Apart from the following two gotchas, everything is working fine.
devise >= 3.1 removed the tokenAuthenticatable module. So I'd declare in my Gemfile:
gem 'devise', '>= 3.0', '< 3.1'
ember-data is drifting away from ActiveModelSerializers, towards json-api. Problem is, json-api itself isn't even stable. The quick and easy fix is to replace DS.RESTAdapter with DS.ActiveModelAdapter, which follows the ActiveModelSerializers conventions. It should "just work".
So, yeah, ember-auth does support rails4, because there is nothing BC-breaking with it per se.
(Previous answer:)
I have no experience with rails 4, but ember-auth itself doesn't rely on rails 3, or in fact rails / devise in particular. The only expectation is a set of API that your server exposes.[1] The docs describe the expectation from the server API.
As for using rails as a backend, ember-data explicitly declares support (and adherence to) active_model_seriailzers, which provides convenience methods for churning out json responses from rails models. However, since authentication actions do not conform to the "standard" RESTful model responses, the ember-auth-rails-demo tutorial itself hand-crafts the expected responses. Example:
def create
# ...
data = {
user_id: resource.id,
auth_token: resource.authentication_token,
}
if params[:remember]
resource.remember_me!
data[:remember_token] = remember_token(resource)
end
render json: data, status: 201
end
So, for rails 4 compatibility, I would investigate more on devise compatibility, any ActiveRecord changes, and in general other gem compatibilities as needed. As for ember-auth, it will still be handcrafting the expected responses, as outlined in the docs.
[1]: Even this expectation would be customizable, by writing customized adapters. Advanced usage, but I can elaborate more on this if needed.
I wanna to add to my blog a counter for displaying, how many times was an article displayed. I would like to solve it through ruby (not GA or something like that).
Is available any gem that do this task (also with checking IP address and the time of latest reading of article) or I have to implement by myself?
There's one used by thousands of people at http://www.seologic.com/webmaster-tools/counters. Source code is available at https://github.com/ivanoblomov/hit_counter.
I am absolutely new to Ruby on Rails, even to programming at all. I got started with Michael Hartl's Rails Tutorial using Rails 3.0.10. Now I alter its aim towards creating an application that allows users to manage their own "projects". These projects are to be exclusively available to the logged-in user, thus, invisible to others.
My problem is: I am unable to create a page with an URL like "~/users/1/projects", I don't know about the routing. All i get done is "~/projects", which is fairly not what i want at all. So, how do I get this problem fixed? Or am I totally off track with that idea?
I generated a Projects model by scaffolding. So, how can I implement it for the signed-in users?
this would be done by creating a nested resource. when you are new to rails and programming you should work yourself a way through a lot of tutorials and guides.
a good place to get an overview are the official rails guides. in this specific case the chapter about routing: http://guides.rubyonrails.org/routing.html#nested-resources
# config/routes.rb
resources :users do
resources :projects
end
This is more for experimentation - I am aware that I can do this with --full but I wanted the functionality of namespacing in the app to avoid conflicts
The idea is to have a main app - which handles authentication, common items, admin screens etc
Then creating engines to add further functionality like
crm
cms
blog
wiki
forum
etc
These engines I can pick and choose as I need for whatever kind of app I am building.
Is this possible?
Is it just the case of applying both --mountable and --full options?
Experimenting - would there be any issue if I use the full option add rspec and then simple add
rails plugin new plugin_name --skip-test-unit --full --dummy-path=spec/dummy
and in lib\plugin_name\engine.rb
module PluginName
class Engine < Rails::Engine
# this is added by rails when an engine is mountable
# to isolate the plugin and prevent name clashes
isolate_namespace PluginName
# one of the additions to make rspec work from command line for the plugin
config.generators do |g|
g.test_framework :rspec, :view_specs => false
end
end
end
I have already created both --full and --mountable engines and have rspec finally working for anyone reading there are some great articles (see below), but wondered of the wider impact of doing this for the solution I am trying to create
I am still playing with this and will post my findings..
Any help/discussion will be massively appreciated.
Please Note
Why I want to do it - build once use many times...
I would never want a non-tech/client to add "plugins/engines" - this is purely to entertain point 1.
Issues I am Having...
Running the server on the top level app. Only when accessing content from the engine, (I can see by the error messages) I have a routing problem (root_path undefined or devise routes missing) - the parent application layout is being rendered, I can see it in the extracted source of the error. Progress but no cigar just yet!
Useful References
Engines vs Mountable Apps
3.1 engines with rspec
testing rails 3 engines
Listing Routes in a Mountable engine
I managed to get this working with the following steps:
In my parent app I was mounting the engine in routes.rb
mount PluginName::Engine => '/plugin_name'
I just removed it.
Created an application controller as Ryan Bigg below had stated.
class PluginName::ApplicationController < ApplicationController
...
end
As I wanted to have things name spaced when generating controllers, models, tests so you have to essentially comment out the isolate_namespace PluginName lib\plugin_name\engine.rb when I wanted the gem to be run in the parent app.
It is not yet an ideal solution. off the top off my head, I could use something like:
isolate_namespace PluginName if %w[development testing].include?(Rails.env)
but will have to test if this is practical.
Kudos to Ryan for helping me find my way many thanks
Furthermore, the same can be done with the --mountable switch version and all you need to do is one further step in your engines config/routes.rb replace
PluginName::Engine.routes.draw do
with
Rails.application.routes.draw do
Yes, you can reference the parent application assets just by referencing them in your application like normal:
<%= stylesheet_link_tag "application %>
Although, not sure why you would want to do that because...
I'm going to answer your first question with the answer to your second question.
To use the application's layout you will need to modify the ApplicationController in the engine (which is namespaced) and have it inherit from ApplicationController in the engine.
That will then have the controllers for the engine using the layout provided by the engine. I'm doing this in my engine, forem.
One day, this will be covered in the Engines Guide that, at this time of writing, is currently being written.
I've got google analytics setup on a rails project, and I've got "A single domain (default)" selected for the tracking options.
I've copied and pasted the js code into the layout for the application.
Now, if I use this locally, does analytics track the local use as well?
The reason I'm asking this is we've been running tests on our dev computers using rspec, and there seems to be a spike in the analytics. All these spikes also seem to show up as unique visitors.
I'd appreciate any insight on this.
Thanks!
Also, using a Google Analytics gem will automatically set some of these features for you. Here is a great way to do it:
Google Analytics and Rails in 5 EASY Steps:
If you are in Rails 3, I just found a great solution for doing Google Analytics in Rails apps.
(1) In your Gemfile:
group :production do
gem 'rack-google_analytics', :require => "rack/google_analytics"
end
(2) Bundle Install
(3) In your config/application.rb (put this in the class definition section - careful not to drop it in a module. I put mine right under "class Application"):
if Rails.env == "production"
config.middleware.use("Rack::GoogleAnalytics", :web_property_id => "UA-0000000-1")
end
(4) Initiate your Google Analytics account
(5) Copy and paste that funky web_property_id from Google's supplied code into the code from (3), replacing 'UA-000000-1'
That's it!
I originally found this solution here:
David Bock Article
I tried the gems but they didn't work; wouldn't spit out any code, etc. Seemed dumb for something so simple. So I ended up just doing this, in application.html.erb:
<% if Rails.env.production? %>
(GA JS Code Snippet)
<% end %>
Yes, it does track local visits as well.
You should probably use ruby conditional statement to exclude it for the local conneciton.
For example, at the bottom of the layout file
<% if !request.local? %>
Your source codes for Google Analytics
<% end %>
This way, Google Analytics will not be printed if connection is made from local.
In the google analytics admin, you can filter out visitors based on various attributes (e.g. ip address) This would also be a good idea to do.
Another option that I've done is add another analytics tracking account that you use for the dev/test environment so that you can test whether and how analytics are working.
This tutorial shows you how to set it up. I don't know why there's a gem for this.
Just put the code in a partial and render it in your application layout if rails.env.production?
http://aihuiong.com/post/452550136/google-analytics-and-rails-in-3-steps-and-less-than-5