mardi 4 août 2015

Android post json to API in background

i have two applications, i have the android app and the Ruby on Rails API. In android i have a SQLite database and almost all the time i need sync the android database with the API database, but this synchronization can take a "long time", something like 10 seconds if is the first sync, so user need to keep waiting and looking to load screen until the process done.

So, i want send a post to the Ruby on Rails application, but without "stop" the application in the load screen, i want to do this sync in background, so the user wont realise that the app is syncing with the API.

Now, i'm trying to working with threads, but it still fails.

Thanks.



via Chebli Mohamed

How to use devise_token_auth with Devise, Angular and Mongoid

I'm trying to use Mongoid, devise, devise_token_auth and ng-token-auth for an token based authorisation for an API written in Rails with Mongoid and Angular as the client.

The problem is when I follow the steps to install devise_token_auth I get an error when I restart my Rails app: undefined methodtable_exists?' for User:Class`

I'm assuming that because I'm using Mongoid the User class don't have the table_exists? method.

How can I get around this? Or, more importantly how can I get this to work?

EDIT: Here's my User class

class User

  include Mongoid::Document
  include Mongoid::Timestamps
  include Mongoid::Enum

  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  ## Database authenticatable
  field :email,              type: String, default: ""
  field :encrypted_password, type: String, default: ""

  ## Recoverable
  field :reset_password_token,   type: String
  field :reset_password_sent_at, type: Time

  ## Rememberable
  field :remember_created_at, type: Time

  ## Trackable
  field :sign_in_count,      type: Integer, default: 0
  field :current_sign_in_at, type: Time
  field :last_sign_in_at,    type: Time
  field :current_sign_in_ip, type: String
  field :last_sign_in_ip,    type: String

  ## Confirmable
  field :confirmation_token,   type: String
  field :confirmed_at,         type: Time
  field :confirmation_sent_at, type: Time
  field :unconfirmed_email,    type: String # Only if using reconfirmable

  include DeviseTokenAuth::Concerns::User

  attr_accessor :reset_token

  enum :role, [:admin, :author]

  after_initialize :set_default_role, :if => :new_record?
  before_create :set_auth_token

  field :first_name,                                        type: String
  field :last_name,                                         type: String
  field :domain,                                                type: String
  field :payment_details,                               type: Hash
  field :subscriber,                                        type: Boolean
  field :stripe_details,                                type: Hash
  field :theme,                                                 type: String

  # Validation
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(?:\.[a-z\d\-]+)*\.[a-z]+\z/i
    before_save { self.email = email.downcase }
    before_create :create_remember_token


  # Get rid of devise-token_auth issues from activerecord
  def table_exists?
    true
  end

  def columns_hash
    # Just fake it for devise-token-auth; since this model is schema-less, this method is not really useful otherwise
    {} # An empty hash, so tokens_has_json_column_type will return false, which is probably what you want for Monogoid/BSON
  end

  def set_default_role
      self.role ||= :admin
  end

end

EDIT 2: Adding stack trace

http://ift.tt/1P4iXd5



via Chebli Mohamed

Passenger Phusion sub-uri with Apache HTTPD without virtualHost Ruby on Rails 4

Trying to deploy my application to a sub-uri. such as, [IP ADDRESS]/sub-domain instead of just [IP address]. so, the instructions at passenger assume that you have a virtualhost already established in your code (http://ift.tt/1IW0ror). Does anybody know how to get this working with the latest version of Passenger and Rails in the HTTPD config file?



via Chebli Mohamed

attr_accessible error on rails 4.1.8 upon performing rake db:migrate on heroku

I am still learning Ruby (so I am a complete noob), right now I have my app successfully running locally but when trying to opening the apps on heroku , in which I first perform the heroku run rake db:migrate I stumbled upon a problem.. it tells me :

Running `rake db:migrate` attached to terminal... up, run.2149
-- attr_accessible(:pName, :pQuantity, :pMeter, :pWeight, :pSellPrice,  :pCategory, :pPic)
-- attr_accessible(:pName, :pQuantity, :pMeter, :pWeight, :pSellPrice, :pCategory, :pPic)
rake aborted!
NoMethodError: undefined method `attr_accessible' for #<ActiveRecord::Migration:0x007f2dc2ba45b8>
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:648:in `block in method_missing'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:621:in `block in say_with_time'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:621:in `say_with_time'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:641:in `method_missing'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:406:in `method_missing'
/app/db/migrate/20150802134246_create_inventories.rb:2:in `<class:CreateInventories>'
/app/db/migrate/20150802134246_create_inventories.rb:1:in `<top (required)>'
/app/vendor/bundle/ruby/2.0.0/gems/activesupport-4.1.8/lib/active_support/dependencies.rb:247:in `require'
/app/vendor/bundle/ruby/2.0.0/gems/activesupport-4.1.8/lib/active_support/dependencies.rb:247:in `block in require'
/app/vendor/bundle/ruby/2.0.0/gems/activesupport-4.1.8/lib/active_support/dependencies.rb:232:in `load_dependency'
/app/vendor/bundle/ruby/2.0.0/gems/activesupport-4.1.8/lib/active_support/dependencies.rb:247:in `require'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:761:in `load_migration'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:757:in `migration'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:752:in `disable_ddl_transaction'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:1044:in `use_transaction?'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:954:in `rescue in block in migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:951:in `block in migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:948:in `each'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:948:in `migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:807:in `up'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:785:in `migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.8/lib/active_record/railties/databases.rake:34:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:migrate

I have been trying to find out the reason, after wondering around I found out about change in rails 4.0.0 in that attr_accessible are no longer used and we should use strong parameter instead, So removing the attr_accessible from model will solve the problem...

However, I have an empty Model, there is no attr_accessible everywhere i look. (beside this is weird why my apps runs locally but not on heroku?) I can't figured out why this error appear and where to look for solutions.. I have been trying to look at active_record file but am afraid of making any changes, any idea?

also, could anyone tell me any resources that can help me read this type of log errors? I have tried to read some articles but can't find one that is easy to understand for noobs like me... ;(



via Chebli Mohamed

Dragonfly images in a polymorphic association syntax?

I have a pictures model:

class Picture < ActiveRecord::Base
  belongs_to :imageable, polymorphic: true
  dragonfly_accessor :image
end

And then I have two different models that should be able to have pictures:

class Teacher < User 
  has_many :pictures, as: :imageable  
end

class Student < User 
  has_many :pictures, as: :imageable
end

I followed instruction here to setup dragonfly and had it working when I just had one model with an image attribute but now that I want to make its own picture model that other models can have_many of then it stops working: http://ift.tt/1P4iXd3

In my rails console I can do something like:

teacher = Teacher.last
teacher.pictures 

and get returned an empty active record proxy:

#<ActiveRecord::Associations::CollectionProxy []>

but I cannot do:

teacher = Teacher.last
teacher.image
teacher.picture.image
teacher.pictures.image

When I try to display on my show view:

<%= image_tag @teacher.pictures.thumb('400x200#').url if @teacher.pictures_stored? %>

I get

undefined method `pictures_stored?'

Even if I delete the if @scientist.pictures_stored? I then get this error: undefined method thumb'

I have tried different combinations since dragonfly gives us the dragonfly_accessor :image on our pictures model. But not sure how to actually reference it. Any help is appreciated.



via Chebli Mohamed

mongoid-4 how to validate uniqueness of belongs_to in 1 to 1 association

I have a 1-to-1 association between 2 mongoid models and I keep getting duplicates, that is having more than one child record(card) with same parent_id(that is user). I have tried validating uniqueness of the belongs_to association has shown below, but it doesn't work.

 class User
   include Mongoid::Document
   field :name, type: String 
   has_one :card
 end

The second model:

 class Card
   include Mongoid::Document
   field :name, type: String 
   belongs_to :user

   validates :user, :uniqueness => {:scope => :user_has_child}

   def user_has_child
     q = Segment.where(drop_id: {'$ne' =>  nil})
     s = q.map(&:drop_id)
     errors.add(:drop_id, "this user already has a card") if s.include?(:drop_id)
   end

 end



via Chebli Mohamed

Modify Devise SAML Attributes

I'm using Rails 4 and Devise with Devise SAML Authenticatable for my Account system.

I've got the SAML working and all, but am trying to work out one thing.

I'd like to change one of the SAML attributes before saving it (since it is formatted incorrectly). Essentially, the Account's SAML request is given a role attribute which is one of the following Group_admin, Group_consumer, Group_supplier. I have a role field in my Account model enumerated as follows:

enum role: [:admin, :consumer, :supplier]

Clearly I can't directly set role because Group_admin != admin (etc.). Is there a way to modify the SAML attribute that is given before Devise saves the field?

I've tried a before_save filter to no avail.

before_save :fix_role!

private
def fix_role!
  self.role = self.role.split('_')[1]
end

Does anyone know of a way to do this? I can post any other code if necessary, I'm just not sure what else is needed. Thanks.



via Chebli Mohamed