Rails 4.0.x with Mongoid.
- MongoDB is used as a datastore with the Mongoid gem for quick development without schemas or migrations.
- Dinamic load of Json Schemas
- rails_admin, for build admin panel.
- RabbitMQ for internal pipeline messages.
- Authentication (via Devise)
- multi-tenantcy with shared-schema database
This is a demonstration application that allows you integrate commerce solutions inspire in Wombat. You can configure the endpoint and see all the data through the data viewer.
Clone the github openjaf/cenit repo and move to cenit folder.
$ git clone https://github.com/openjaf/cenit $ cd cenit
Change branch to develop.
$ git checkout -b develop origin/develop
Ensure stay in develop branch.
$ git branch > *develop
Ensure that ‘develop’ branch is updated.
$ git pull origin develop > Already up-to-date.
Run the bundle install command to install the required gems on your computer:
$ bundle install
With mongodb you don’t need run migrations
Run db/seed
$ rake db:seed
Run cenit hub by port: 3000
$ rails s -p 3000
If you have some trouble with secret_key_base running ‘rails s’, you can generate a random secret key value:
$ rake secret
Then take this value and put it in config/initializers/secret_token.rb:
Cenit::Application.config.secret_key_base = 'bla' # replace this
Now visit /data, for enter in data you need sing in before. If you don’t have user yet, clic in sing up.
If you want unavailable ‘sing up’ change user model.
go to http://localhost:3000/data
If you have some trouble for enter in /data check that mondodb is running.
Check that you have a working installation of RabbitMQ, see below the guide for install RabbitMQ.
If RabbitMQ is well installed when you run rails server then you can see:
[*] Waiting for messages. To exit press CTRL+C
You should see something like
Clone the github openjaf/hub-demo-store repo.
$ git clone https://github.com/openjaf/hub-demo-store
Change folder name to hub-demo-store-1 and move to folder.
$ mv hub-demo-store hub-demo-store-1 $ cd hub-demo-store-1
Change branch to cenit.
$ git checkout -b cenit origin/cenit
Ensure stay in ‘cenit’ branch
$ git branch > *cenit
Ensure that ‘cenit’ branch is updated.
$ git pull origin cenit > Already up-to-date.
Run the bundle install command to install the required gems on your computer:
$ bundle install
Add database.yml file to config/
development: adapter: sqlite3 database: db/development.sqlite3 pool: 5 timeout: 5000 test: adapter: sqlite3 database: db/test.sqlite3 pool: 5 timeout: 5000 production: adapter: sqlite3 database: db/production.sqlite3 pool: 5 timeout: 5000
Create db, run migrations and load seed data.
$ rake db:create; rake db:migrate; rake db:seed
Run hub-demo-store-1 by port 3001
$ rails s -p 3001
Clone the github openjaf/hub-demo-store repo.
$ git clone https://github.com/openjaf/hub-demo-store
Change folder name to hub-demo-store-2 and move to folder.
$ mv hub-demo-store hub-demo-store-2 $ cd hub-demo-store-2
Change branch to cenit.
$ git checkout -b cenit origin/cenit
Ensure stay in ‘cenit’ branch
$ git branch > *cenit
Ensure that ‘cenit’ branch is updated.
$ git pull origin cenit > Already up-to-date.
Run the bundle install command to install the required gems on your computer:
$ bundle install
Create database.yml.
vim config/database.yml
Copy here you config
development: adapter: sqlite3 database: db/development.sqlite3 pool: 5 timeout: 5000 test: adapter: sqlite3 database: db/test.sqlite3 pool: 5 timeout: 5000 production: adapter: sqlite3 database: db/production.sqlite3 pool: 5 timeout: 5000
Create db, run migrations and load seed data.
$ rake db:create; rake db:migrate; rake db:seed
Run hub-demo-store-1 by port 3002
$ rails s -p 3002
Move to cenit hub folder
$ cd ../cenit
Stop the web server by port 3000
Ctrl + c
Run rails console
$ rails console
Add webhooks for Product Object at this moment only ‘add_product’ and ‘update_product’.
$ w_add = Setup::Webhook.create!({:name => 'Add Product', :path => 'add_product'}) $ w_upd = Setup::Webhook.create!({:name => 'Update Product', :path => 'update_product'})
Add conection configurations to Store I and Store II
$ c_one = Setup::Connection.create!({:name => 'Store I', :url => 'http://localhost:3001/wombat', :store => '3001', :token => 'tresmiluno'}) $ c_one.webhooks = [w_add, w_upd] $ c_one.save $ c_two = Setup::Connection.create!({:name => 'Store II', :url => 'http://localhost:3002/wombat', :store => '3002', :token => 'tresmildos'}) $ c_two.webhooks = [w_add, w_upd] $ c_two.save
Stop rails console
Ctrl + c
Run cenit hub by port: 3000
$ rails s -p 3000
Move to hub-demo-store-1
$ cd ../hub-demo-store-1
Stop the web server by port 3001
Ctrl + c
Edit file hub-demo-store-1/config/initializers/wombat.rb
Spree::Wombat::Config.configure do |config| config.connection_id = "3001" config.connection_token = "tresmiluno" config.push_objects = ["Spree::Order", "Spree::Product"] config.payload_builder = { "Spree::Order" => {:serializer => "Spree::Wombat::OrderSerializer", :root => "orders"}, "Spree::Product" => {:serializer => "Spree::Wombat::ProductSerializer", :root => "products"}, } config.push_url = "http://localhost:3000/cenit" config.enable_auto_push = true end
Run hub-demo-store-1 by port 3001
$ rails s -p 3001
Move to hub-demo-store-2
$ cd ../hub-demo-store-2
Stop the web server by port 3002
Ctrl + c
Edit file hub-demo-store-2/config/initializers/wombat.rb
Spree::Wombat::Config.configure do |config| config.connection_id = "3002" config.connection_token = "tresmildos" config.push_objects = ["Spree::Order", "Spree::Product"] config.payload_builder = { "Spree::Order" => {:serializer => "Spree::Wombat::OrderSerializer", :root => "orders"}, "Spree::Product" => {:serializer => "Spree::Wombat::ProductSerializer", :root => "products"}, } config.push_url = "http://localhost:3000/cenit" config.enable_auto_push = true end
Run hub-demo-store-2 by port 3002
$ rails s -p 3002
At the bigining you don’t have any product in the Cenit Hub
Go to browser and visit products in the admin panel of Hub Demo Store 1.
# go to http://localhost:3001/admin/products
Enter the credentials
# user: [email protected] # pass: spree123
Create a new product in the product form with the next values.
Product name: 'Product 1' Sku: '111' Price: 100 Shipping Category: Default <pre> <img src="app/assets/images/cenit_demo_integration_2.png" alt="Cenit Demo Integration image 2" /> When you make clic in the button 'create', after save in the database the product, this will be send it to Cenit Hub,, through a Push API /add_product that is a HTTP Post request. The Cenit Hub will process the HTTP Post request, and it will create a new product in the MongoDB. Now you can visit /data in Cenit Hub and check that the product was saved. <pre> go to http://localhost:3000/data
After save the new product, then will be sent a internal message using RabbitMQ because a new product was created and is required notify the rest of endpoint(s).
When the rabbitMQ message had been processed, the Cenit call the webhook /add_product for each conection, in this case will be send a request HTTP Post /add_product to Hub Demo Store II.
The Store II will process the HTTP Post request, and it will create a new product in your DB.
You can visit the other Storefront and check that the product was saved too.
go to http://localhost:3002
Now goes to the admin in Hub Demo Store II, and modified the product name.
Product name: 'Product 111'
When you click the button ‘update’, after save in the databse the product will be send to Cenit Hub, throgth a Push API /add_product that is a HTTP Post request.
Now you can visit /data in Cenit Hub and check that the product modification was saved.
go to http://localhost:3000/data
After save the new product, then will be sent a internal message using RabbitMQ because a product was updated and is required notify the rest of endpoint(s).
When the rabbitMQ message had been processed, the Cenit call the webhook /update_product for each conection, in this case will be send a request HTTP Post /update_product to Hub Demo Store I.
The Store I will process the HTTP Post request, and it will create a new product in your DB.
You can visit the other Storefront and check that the product was saved too.
Then like a last prove, go to Cenit Hub and modified a product in data.
Product name: 'Product 111 111'
How you spec and both endpoint will be updated too.
Before generating your application, you will need:
- The Ruby language (version 1.9.3 or 2.0.0)
- The Rails gem (version 4.0.5)
- A working installation of MongoDB
- A working installation of RabbitMQ
If you don’t have MongoDB installed on your computer, you’ll need to install it and set it up to be always running on your computer (run at launch).
On Mac OS X, the easiest way to install MongoDB is to install Homebrew and then run the following:
brew install mongodb
Homebrew will provide post-installation instructions to get MongoDB running. The last line of the installation output shows you the MongoDB install location (for example, /usr/local/Cellar/mongodb/1.8.0-x86_64). You’ll find the MongoDB configuration file there. After an installation using Homebrew, the default data directory will be /usr/local/var/mongodb.
On the Debian GNU/Linux operating system, as of March 2013, the latest stable version is MongoDB 2.0.0. With MongoDB 2.0.0, the Mongoid gem must be version 3.0.×. See the Mongoid installation instructions. Change your Gemfile to use an earlier Mongoid version:
gem 'mongoid', github: 'mongoid/mongoid' gem 'bson_ext', '~> 1.8.6'
The RabbitMQ site has a good installation guide that addresses many operating systems. On Mac OS X, the fastest way to install RabbitMQ is with Homebrew:
brew install rabbitmq
then run it:
rabbitmq-server
On Debian and Ubuntu, you can either download the RabbitMQ .deb package and install it with dpkg or make use of the apt repository that the RabbitMQ team provides.
For RPM-based distributions like RedHat or CentOS, the RabbitMQ team provides an RPM package.
Note: The RabbitMQ packages that ship with Ubuntu versions earlier than 11.10 are outdated and will not work with Bunny 0.9.0 and later versions (you will need at least RabbitMQ v2.0 for use with this guide).
You have several options for getting the code.
If you’d like to add features (or bug fixes) to improve the example application, you can fork the GitHub repo and make pull requests. Your code contributions are welcome!
If you want to copy and customize the app with changes that are only useful for your own project, you can clone the GitHub repo. You’ll need to search-and-replace the project name throughout the application. You probably should generate the app instead (see below). To clone:
$ git clone https://github.com/openjaf/cenit
Use the default WEBrick server for convenience. If you plan to deploy to Heroku, select “thin” as your production webserver.
Set a robots.txt file to ban spiders if you want to keep your new site out of Google search results.
It is a good idea to use rvm, the Ruby Version Manager, and create a project-specific rvm gemset (not available on Windows). See Installing Rails.
If you choose to create a GitHub repository, the generator will prompt you for a GitHub username and password.
Other problems? Check the "issues"https://github.com/openjaf/cenit.
If you’re storing the app in a GitHub repository, please edit the README files to add a description of the app and your contact info. If you don’t change the README, people will think we are the author of your version of the application.
Check the Gemfile to see which gems are used by this application.
$ bundle install
You can check which gems are installed on your computer with:
$ gem list
Keep in mind that you have installed these gems locally. When you deploy the app to another server, the same gems (and versions) must be available.
I recommend using rvm, the Ruby Version Manager, to create a project-specific gemset for the application. See the article Installing Rails.
Mongoid provides access to the MongoDB database from Rails.
You can use the default configuration found in the file config/mongoid.yml.
If you want to see what’s in your MongoDB databases, I recommend using the MongoHub app (for Mac OS X).S
The application uses the figaro gem to set environment variables. Credentials for your administrator account and email account are set in the config/application.yml file. The .gitignore file prevents the config/application.yml file from being saved in the git repository so your credentials are kept private. See the article Rails Environment Variables for more information.
Modify the file config/application.yml:
# Add account credentials and API keys here. # See http://railsapps.github.io/rails-environment-variables.html # This file should be listed in .gitignore to keep your settings secret! # Each entry sets a local environment variable and overrides ENV variables in the Unix shell. # Add application configuration variables here, as shown below.
If you wish, set your name, email address, and password for the first user’s account. If you prefer, you can use the default to sign in to the application and edit the account after deployment. It is always a good idea to change the password after the application is deployed.
All configuration values in the config/application.yml file are available anywhere in the application as environment variables. For example, ENV["GMAIL_USERNAME"]
will return the string “Your_Username”.
If you prefer, you can delete the config/application.yml file and set each value as an environment variable in the Unix shell.
The db/seeds.rb file initializes the database with default values. To keep some data private, and consolidate configuration settings in a single location, we use the config/application.yml file to set environment variables and then use the environment variables in the db/seeds.rb file.
Note that it’s not necessary to personalize the db/seeds.rb file before you deploy your app. You can deploy the app with an example user and then use the application’s “Edit Account” feature to change name, email address, and password after you log in. Use this feature to log in as the first user and change the user name and password to your own.
You may wish to include additional sample users:
Prepare the database and add the default user to the database by running the commands:
$ rake db:seed
Use rake db:reseed
if you want to empty and reseed the database. Or you can use rake db:drop
and rake db:setup
. The equivalent task for Rails with ActiveRecord is rake db:reset
which will be available in Mongoid 4.0.
Set the database for running tests:
$ rake db:test:prepare
If you’re not using rvm, the Ruby Version Manager, you should preface each rake command with bundle exec
. You don’t need to use bundle exec
if you are using rvm version 1.11.0 or newer.
If you’ve used the Rails Composer tool to generate the application, the application’s secret token will be unique, just as with any Rails application generated with the rails new
command.
However, if you’ve cloned the application directly from GitHub, it is crucial that you change the application’s secret token before deploying your application in production mode. Otherwise, people could change their session information, and potentially access your site without your permission. Your secret token should be at least 30 characters long and completely random.
Get a unique secret token:
You can check that your app runs properly by entering the command
$ rails server
To see your application in action, open a browser window and navigate to http://localhost:3000/. You should see the default user listed on the home page. When you click on the user’s name, you should be required to log in before seeing the user’s detail page.
You should delete or change the pre-configured logins before you deploy your application.
If you test the app by starting the web server and then leave the server running while you install new gems, you’ll have to restart the server to see any changes. The same is true for changes to configuration files in the config folder. This can be confusing to new Rails developers because you can change files in the app folders without restarting the server. Stop the server each time after testing and you will avoid this issue.
For your convenience, here are instructions for deploying your app to Heroku. Heroku provides low cost, easily configured Rails application hosting.
Devise provides a variety of features for implementing authentication. See the Devise documentation for options.
This example application and tutorial demonstrates Devise and Mongoid working together on Rails 4. Add any models, controllers, and views that you need.
The example application contains a suite of RSpec unit tests and Cucumber scenarios and step definitions.
After installing the application, run rake -T
to check that rake tasks for RSpec and Cucumber are available.
Run rake spec
to run RSpec tests.
Run rake cucumber
(or more simply, cucumber
) to run Cucumber scenarios.
Please send the author a message, create an issue, or submit a pull request if you can contribute improved RSpec or Cucumber files.
Problems? Check the issues.
For a Mongoid introduction, Ryan Bates offers a Railscast on Mongoid. You can find documentation for Mongoid at http://mongoid.org/ There is an active Mongoid mailing list and you can submit Mongoid issues at GitHub.
For a Devise introduction, Ryan Bates offers a Railscast on Devise. You can find documentation for Devise at http://github.com/plataformatec/devise. There is an active Devise mailing list and you can submit Devise issues at GitHub.
Please create an issue on GitHub if you identify any problems or have suggestions for improvements.
Your best source for help with problems is Stack Overflow. Your issue may have been encountered and addressed by others.
You can also try Rails Hotline, a free telephone hotline for Rails help staffed by volunteers.
If you make improvements to this application, please share with others.
Send the author a message, create an issue, or fork the project and submit a pull request.
If you add functionality to this application, create an alternative implementation, or build an application that is similar, please contact me and I’ll add a note to the README so that others can find your work.
Many of the concepts here develop was inspired by Wombat.
Thank you for improvements to the application: