#rails4
Explore tagged Tumblr posts
Text
So i have a Rails 4.1.x app running on ruby 2.2.x, which only runs on my older laptop because I'm still using rbenv on that. asdf doesnt seem to want to install anything older than ruby 2.4.x
so now I'm on a race with myself to get it upgrades to the last version of rails4 and ruby 2.5.x. I have no tests. this will kill me.
0 notes
Text
Chatter is an enterprise social network and collaboration environment. Force.com exposes various useful information from Chatter such as users, organizations, groups, feed-items through APIs. Using this information, we can build a proof-of-concept dashboard which will show a user’s or organization’s feed in real-time. Real-time dashboard can provide an accurate understanding of what is happening in an organization. This tutorial expects that you are an intermediate-level web application developer and have a few weeks of experience with Rails and related ecology. This means you should be familiar with building blocks of a Rails app and terms like OAuth, REST, Callback, bundler, gem etc. Here is an outline of how things will work: User can login to our Rails4 powered dashboard (a connected app) using ‘Sign in with Salesforce’ (Oauth 2). We use OAuth to get a secret token for each user from salesforce.com. We can use the token to call APIs. Our goal is to receive a callback to server whenever anything is posted on Chatter. Unfortunately, Force.com doesn’t support a PushTopic for FeedItem, so we will use a work-around to trigger a callback whenever a FeedItem is created. First, we will create a trigger on FeedItem, this trigger will create a custom object named ProxyFeedItem, which will copy necessary fields like body, time, parent_id etc. from the FeedItem. Using a faye client embedded in restforce client, we will listen to a PushTopic for ProxyFeedItem. ProxyFeedItem will be created whenever there’s an update to any FeedItem. This will send a callback to the server with data of the ProxyFeedItem. We will need to forward this incoming data to user’s browser. We will set up another faye channel and just transfer the data we received in step 4. First, go to https://developer.salesforce.com/signup and register for your free Developer Edition (DE) account. For the purposes of this example, I recommend sign up for a Developer Edition even if you already have an account. This ensures you get a clean environment with the latest features enabled. After sign up, make a connected app by following the directions found in this article from the salesforce.com developer portal. Use http://localhost:3000 as Start URL, enable Oauth settings, select appropriate permissions and use http://localhost:3000/oauth/salesforce/callback as callback URL. When you create your app, you will get the app’s Consumer Key and Consumer Secret. We have set up everything that we need from Force.com for this section and we can move on to our web application code. Create a new Rails4 application with rails new chatter-dashboard This will go ahead and create a Rails4 project with the name ‘chatter-dashboard’ and install the dependencies mentioned in Gemfile. Actually, we need a few more dependencies. Change Gemfile to the following: source 'https://rubygems.org' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' gem 'rails', '4.1.0' # Use sqlite3 as the database for Active Record gem 'sqlite3' # Use SCSS for stylesheets gem 'sass-rails', '~> 4.0.3' # Use Uglifier as compressor for JavaScript assets gem 'uglifier', '>= 1.3.0' # Use CoffeeScript for .js.coffee assets and views gem 'coffee-rails', '~> 4.0.0' # See https://github.com/sstephenson/execjs#readme for more supported runtimes # gem 'therubyracer', platforms: :ruby # Use jquery as the JavaScript library gem 'jquery-rails' # Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks gem 'turbolinks' # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder gem 'jbuilder', '~> 2.0' # bundle exec rake doc:rails generates the API under doc/api. gem 'sdoc', '~> 0.4.0', group: :doc # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring gem 'spring', group: :development # Use ActiveModel has_secure_password # gem 'bcrypt', '~> 3.1.7'
# Use unicorn as the app server # gem 'unicorn' # Use Capistrano for deployment # gem 'capistrano-rails', group: :development # Use debugger # gem 'debugger', group: [:development, :test] # Using customized version to fix issue #103 in restforce gem 'restforce', :git => '[email protected]:malavbhavsar/restforce.git', :branch => 'patch-1' # Use omniauth for handlling OAuth with Salesforce gem 'omniauth' # Add omniauth policy for saleforce gem 'omniauth-salesforce' # Print pretty gem 'awesome_print' # Development only gems group :development do gem 'better_errors' gem 'binding_of_caller' end # Add faye for pub/sub, using customized version to avoid problems from # issue 263 and other related issue gem 'faye', :git => '[email protected]:faye/faye.git' # private_pub to easily do pub-sub with browser, using customized version # to make sure that we get faye.js which is not packed when using faye gem # from master gem 'private_pub', :git => '[email protected]:malavbhavsar/private_pub.git' # Puma for our main server concurrently gem 'puma' # Thin for running faye server gem 'thin' Run bundle install, which will install additional dependencies. To start our server, run rails s puma; this will run rails with a puma server and you should be able to see a welcome page on http://localhost:3000. The next step is to set up Oauth with salesforce.com. Add Consumer Key and Consumer Secret to chatter-dashboard/config/secrets.yml development: secret_key_base: salesforce_key: salesforce_secret: test: secret_key_base: # Do not keep production secrets in the repository, # instead read values from the environment. production: secret_key_base: Create a chatter-dashboard/config/initializers/omniauth.rb file and add the following code into it: Rails.application.config.middleware.use OmniAuth::Builder do provider :salesforce, Rails.application.secrets.salesforce_key, Rails.application.secrets.salesforce_secret , :scope => "id api refresh_token" end This configures our omniauth and omniauth-salesforce gems. It has basically added a middleware in our Rails application, which will handle Oauth for us. You can read the documentation for these gems to dig deeper. Now, run the following commands to set up two controllers and relevant routes; one for the login page and the other for the feed page: rails g controller Login login rails g controller Feed feed Now, in the chatter-dashboard/config/routes.rb file, add the following routes: get '/auth/:provider/callback', to: 'sessions#create' root to: 'login#login' This will basically add a callback route to which the user will be redirected to by Force.com after the Oauth procedure has finished successfully. We have also added a root route, so that whenever we go to http://localhost:3000, it will trigger the login#login route. Currently, it’s just an empty page. Let’s add a ‘Sign in with Salesforce’ link to it. Add the following line to chatter-dashboard/app/views/login/login.html.erb: If you hit refresh and click on ‘Sign in with Salesforce’, you will be taken to the login page of salesforce.com if you are not signed in. After signing in and giving the app permissions, you will be redirected to http://localhost:3000/auth/salesforce/callback, but we haven’t implemented matching sessions#create yet. Let’s do that by doing rails g controller Sessions create. For the implementing create method, use the following code: class SessionsController < ApplicationController def create set_client ap @client redirect_to '/feed/feed' end protected def auth_hash_credentials request.env['omniauth.auth'][:credentials] end def set_client @client = Restforce.new :oauth_token => auth_hash_credentials[:token], :refresh_token => auth_hash_credentials[:refresh_token], :instance_url => auth_hash_credentials[:instance_url], :client_id => Rails.application.secrets.salesforce_key, :client_secret => Rails.application.secrets.salesforce_secret end end Here, we parse the callback request coming from Force.
com and get oauth_token, refresh_token etc and create a restforce client. If you see something like the following in your console, then you have completed the first section of this tutorial: In the first section of the tutorial, we set up Oauth with salesforce.com and created a restforce client object. In this section, we want Force.com to notify us of any changes in the FeedItem object of Chatter. Unfortunately, Salesforce streaming API doesn’t support FeedItem yet, so we will have to do a work-around. Create a custom object named ProxyFeedItem. Add necessary fields like Body, Type, CommentCount, LikeCount, CreatedById from FeedItem Now, let’s setup a trigger on FeedItem. You can do this by going to ‘Setup’ on your Force.com and search for ‘FeedItem Trigger’. Use the following code: trigger FeedItemListen on FeedItem (after insert, after update) for(FeedItem f : Trigger.new) ProxyFeedItem__c p = new ProxyFeedItem__c(Body__c = f.Body, CommentCount__c = f.CommentCount, LikeCount__c = f.LikeCount, Type__c = f.Type, User__c = f.CreatedById); insert p; Whenever this is triggered, we get the data from Trigger.new iterate over it and create our custom object ProxyFeedItem for each FeedItem in the data. Now, we have to create a PushTopic, which will listen to any changes in all ProxyFeedItem (and in turn FeedItem) We will subscribe to this PushTopic and send the changes to browser. Following the streaming example given in the restforce docs, we can create a file at chatter-dashboard/lib/chatter_listen.rb like the following: module ChatterListenEM def self.start(client) pushtopics = client.query('select Name from PushTopic').map(&:Name) unless pushtopics.include?('AllProxyFeedItem') client.create! 'PushTopic', ApiVersion: '30.0', Name: 'AllProxyFeedItem', Description: 'All ProxyFeedItem', NotifyForOperations: 'All', NotifyForFields: 'All', Query: "SELECT Id, Body__c, CommentCount__c, LikeCount__c, Type__c, User__c from ProxyFeedItem__c" end Thread.abort_on_exception = true Thread.new EM.run do client.subscribe 'AllProxyFeedItem' do die_gracefully_on_signal end def self.die_gracefully_on_signal Signal.trap("INT") EM.stop Signal.trap("TERM") EM.stop end end Whenever ChatterListenEM.start is called, it creates a PushTopic named ChatterFeedItem, if it doesn’t already exist. Next, it creates a new thread and subscribes to that PushTopic in it. Whenever we receive a message, we pass it a Faye channel e.g. messages/new using private_pub. private_pub is a ruby gem, which makes it easier to setup a pub-sub type mechanism between a web server and browser. You can learn more about it in this screencast on private pub Before going to private_pub and related stuff, let’s call our ChatterListenEM.start method from SessionController. There is just one minor change: require 'chatter_listen' class SessionsController < ApplicationController def create set_client ChatterListenEM.start(@client) redirect_to '/feed/feed' end protected def auth_hash_credentials request.env['omniauth.auth'][:credentials] end def set_client @client = Restforce.new :oauth_token => auth_hash_credentials[:token], :refresh_token => auth_hash_credentials[:refresh_token], :instance_url => auth_hash_credentials[:instance_url], :client_id => Rails.application.secrets.salesforce_key, :client_secret => Rails.application.secrets.salesforce_secret end end Now, let’s set up private_pub. Run rails g private_pub:install on console. It will create and place necessary files like private_pub.ru, private_pub.yml and faye.js, private_pub.js in asset-pipeline. To make rails aware of faye.js and private_pub.js files, add them to the chatter-dashboard/app/assets/javascripts/application.js file. // This is a manifest file that'll be compiled into application.js, which will include all the files // listed below. // // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts, // or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
// // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the // compiled file. // // Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details // about supported directives. // //= require jquery //= require jquery_ujs //= require faye //= require private_pub //= require turbolinks //= require_tree . Start our Faye server in a different console. This will handle pub-sub for us. rackup private_pub.ru -s thin -E production All that is left to do now is to subscribe to the channel /messages/new and print our data. We can take easy examples from the private_pub documentation and add the following to our chatter-dashboard/app/views/feed/feed.html.erb: and the following to our chatter-dashboard/assets/javascripts/feed.js: PrivatePub.subscribe("/messages/new", function(data, channel) console.log(data.chat_message); ); Now, go to http://localhost:3000, ‘Login with Salesforce’ and you will end up on the feed page. Open the developer console and in another tab open the Chatter tab of salesforce.com. If you do a text post, you will be able to see a real time update in the console. Here’s a proof of concept, showing the dashboard in action: We just implemented a system like below: Instead of printing data in console, you can easily feed it into any frontend framework like angular, ember etc. and create a great real-time dashboard. We also have left out few things in this proof-of-concept prototype e.g. we have to secure our faye channels. One way of doing this is creating a different channel for each user. e.g. /messages/new/user_id and subscribe the user only to that particular channel. Additionally, use SSL. If you are handling any real user data, it is important that you secure the data being transferred. Force.com makes sure to secure the data and only provides developers with data over SSL using OAuth. It is however the responsibility of the developer to ensure secure communication in any RESTful app. For more information, you should refer to Security Resources. You can find the code for this project at github chatter-dashboard Resources For a comprehensive set or resources, check out: About The Author: This article is created by Malav Bhavsar. Please feel free to ask questions in the comment section, open issues in the github repository or contact me at [email protected]
0 notes
Photo

Comprehensive Ruby on Rails ☞ https://goo.gl/1t8Q8X #Rails4 #RubyonRails
2 notes
·
View notes
Text
管理画面のRailsバージョンをRails4からRails5に上げた話 - Gunosy Tech Blog [はてなブックマーク]
管理画面のRailsバージョンをRails4からRails5に上げた話 - Gunosy Tech Blog
2018 - 02 - 26 管理画面のRailsバージョンをRails4からRails5に上げた話 Rails こんにちは、広告技術部の倉澤です。 普段は広告配信のための管理画面を作ったりしています。 今回はその管理画面をRails5にアップデートした話を書きたいと思います。 この管理画面の first commit は2013年9月17日、 Rails Wayにそってないコードもあり、それなり...
kjw_junichi あとで読む
from kjw_junichiのブックマーク http://ift.tt/2oxxND7
0 notes
Text
Ruby Ralis Developer
[ad_1] Gob title: Ruby Ralis Developer Company: Gob description: Ruby Ralis Developer Requirements in MNC- Clients Of SV MAnagement Consultancy Firm Work Experience 1 – 6 Year(s) Functional Area IT Role Software Engineer/ Programmer Industry IT-Software / Software Services Keywords Rails3 ,Rails4, j… Expected salary: Location: Indore, Madhya Pradesh Job date: Tue, 04 Dec 2018 03:37:37 GMT Apply for…
View On WordPress
0 notes
Photo

Comprehensive Ruby on Rails ☞ https://goo.gl/1t8Q8X #Rails4 #RubyonRails
0 notes
Photo
rails4のinitializerの読み込み順序を検証 via Pocket https://ift.tt/2Jke82j
0 notes
Link
Rails4以前ではbelongs_to関連��外部キーでnilを許可しないようにするためには、required: true を入れる必要があったが、Rails5ではデフォルトでnilを許可しない仕様に変更された。
required: falseにしたい時はoptional: trueと書けるようになる。
models/article.rb class Article < ActiveRecord::Base belongs_to :user, optional: true end
0 notes
Text
ハイレベル・プログラミングスクールの「DIVE INTO CODE」、Rails技術者認定試験認定の問題集アプリケーション「DIVE INTO EXAM」をローンチ!
ハイレベル・プログラミングスクールの「DIVE INTO CODE」、Rails技術者認定試験認定の問題集アプリケーション「DIVE INTO EXAM」をローンチ!
[株式会社Dive into Code] ■DIVE INTO CODE「DIVE INTO EXAM」とは 一般社団法人Rails技術者認定試験運営委員会認定のオンライン・プログラミング問題集アプリケーションです。Rails4技術者認定模擬試験などの最新の問題を本番と同じスタイ… Source: PR TIMES
View On WordPress
0 notes
Text
Can ah?
I'm upgrading a Rails 4 app to Rails 5, I used to be a big fan of declarative_authorization, and used it in all of my early apps.
By the time I required any sort of role based authorisation, even CanCan had been deprecated by CanCanCan.
Anyway back to the 4 to 5 upgrade, the declarative_authorization gem no longer works with Rails 5, and the alleged successor forks, I couldn't get to work either. So I am migrating to CanCanCan. So far it has been a relatively painless process, although I'm actually learning a bit deeper about CanCanCan, as declarative_authorization is supremely granular, and I never really had to do granular authorization with CanCanCan. The main issue now is changing;
permitted_to? :auth, :action do block end
to
if can? auth, :action
and in controllers
filter_access_to :controller_actions
to
load_and_authorize_resource
Still got a long way to go.... and 4 more apps to go. Wonder if I can upgrade 5 to 7 though as Rails 6 was kind of a POS. lol
0 notes
Photo
Easy Caching with Rails 4 + Heroku + Redis ☞ https://medium.com/ruby-on-rails/easy-caching-with-rails-4-heroku-redis-5fb36381628 #Rails4
0 notes
Text
DynamoDB で TTL が実装されたので、Rails4 から使ってみた
ブログを更新しました > https://masutaka.net/chalow/2017-03-02-1.html
0 notes
Text
Rails 4: POST リクエストを扱う
Rails 4: Hello World
Rails 4: JSON、プレーンテキストをレスポンスとして返す
Rails 4: POST リクエストを扱う
Rails 4: Basic 認証
Rails 4: モデル
HTTP メソッドの種類を config/routes.rb で指定する必要がある。
post 'hello' => 'hello#index'
複数の HTTP メソッドに対応するのであれば、match メソッドを使うことができる。
match 'hello' => 'hello#index', :via => [:get, :post]
POST メソッドに対応できるようにするために CSRF を無効にする。コントローラーのすべてのアクションで CSRF を無効にするには skip_before_filter を呼び出す。
class HelloController < ApplicationController skip_before_filter :verify_authenticity_token def index render plain: params[:foo] == 'bar' ? 'ok' : 'fail' end end
CSRF を無効にするには protect_from_forgery を使うことができる。
class HelloController < ApplicationController protect_from_forgery except: :index end
httpie のコマンドで POST リクエストを試してみよう。CSRF を無効にしていない場合、422 Unprocessable Entit エラーになる。
http localhost:3000/hello foo=bar
JSON リクエストも自動的に解析される。httpie で JSON を POST メソッドで投稿してみよう。
http localhost:3000/hello foo=bar baz=qux
params から想定していない値を取り出せないようにするには permit メソッドを使うことができる。
def index render plain: params.permit(:foo) end
require メソッドの場合、指定した値が含まれない場合、例外が投げられる。
def index render plain: params.require(:foo) end
今度は CSRF 対策のトークンを送信してみよう。トークンを得るには form_authenticity_token を使う。このメソッドを呼び出すと、セッション Cookie も発行されるので、トークンと一緒に送信する必要がある。
まずは POST を受けつけるアクションを config/routes.rb で指定しよう。
Rails.application.routes.draw do get 'hello/index' get 'hello' => 'hello#index' match 'hello/create' => 'hello#create', :via => [:get, :post] end
次にコントローラーを修正する。
class HelloController < ApplicationController def index render plain: render plain: request_forgery_protection_token.to_s + ': ' + form_authenticity_token end def create render plain: 'ok' end end
次の URL から CSRF 対策のトークンとセッション Cookie を発行してもらう。 次の例では httpie を使っている。
http localhost:3000/hello
POST リクエストを発行して、レスポンスの HTTP ステータスコードが 200 になるか確認してみよう。Cookie と 'X-CSRF-Token ヘッダーに指定する値は先ほどの GET リクエストで調べたものを使う。
http --headers localhost:3000/hello/create 'Cookie: _myproject_session=NDZFUjkyOC9qMUR2UUZ6VGxWOEE3T0x2cDlmdkk3a2FIWWc2bTBBMXdoakpId3l2dTlLdnR1WWQ5SXdoQkdtSlBKWFRuTmthT1RxU3BtV0drZTRhOUxIK29DWXVYLzZQS0JXVFZsanlVWEhpanB5N1BIT0k3bVJRTDdldm5LZjU2cnBxdDFxVkdqSi8zeEFINk1tV1NnPT0tLWF4VHR2eG15eFJqVmxXSHR4eUVUbmc9PQ%3D%3D--bcdfe477cbd64a5a2eb279da229b40334f258ce9; path=/; HttpOnly' 'X-CSRF-Token: JjyGuDi2WOL4RgEhVQG3DV0IfMVYufLz8qSUkwJRGF0='
X-CSRF-Token ヘッダーの代わりにリクエストボディに含めることができる。パラメーターの名前は authenticity_token になる。
http --headers localhost:3000/hello/create 'Cookie: _myproject_session=MWRFeXRveDZaRlQ0Vm5EWFByZno5Vy8yWGxlZ2FoaGRBaEhZeFRpVnhzODkwNzhqeTQxWFNpc1dCcnZsdkM3d1g3Y3htY21mOVMySDd1NDJ4OTZHdU1ubGoyVDUzR3JQR015WHpNNCtmeHAzQ2FyR3NQN2lITlVOVlJrU2RiTEtDSlIvSmFlNVpZanA3a09kRUp6QTl3PT0tLUxyQk8zeHl6bUlFd0xJMkgyZmNGMlE9PQ%3D%3D--22689f9dfd46aa6b9f3d5a62da1d66f00dbddc0f; path=/; HttpOnly' authenticity_token=OVsarDTtFB65SeGOOrHzgSL7fvuUKR543xRC98K2+XA=
2 notes
·
View notes