Is there a gem to manage settings per resource? - ruby-on-rails-3

I need a gem that can abstract resource setting management. Basically I want something like
#person = Person.find(1)
#person.settings <- this gives a hash of key/value pairs associated with this resource
I also need a way to have "default" settings per Person as well as a way to override those for specific #person. The settings should be persisted in SQL db.

This is an old plugin but it's pretty full featured and I've used it in a number of different projects: has_easy
For Rails3 the generator won't work but you can just create the migration it needs by hand.
class CreateHasEasyThings < ActiveRecord::Migration
def self.up
create_table :has_easy_things do |t|
t.string :model_type, :null => false
t.integer :model_id, :null => false
t.string :context
t.string :name, :null => false
t.string :value
t.timestamps
end
end
def self.down
drop_table :has_easy_things
end
end
Basically the way it works is that you can associate any model you want with this object and it will store preferences, settings or really pretty much anything that can be serialized by Rails.
You define your settings or what have you on the model:
class User < ActiveRecord::Base
has_easy :settings do |p|
p.define :language
p.define :theme
end
has_easy :flags do |f|
f.define :is_admin
f.define :is_spammer
end
end
which dynamically creates a bunch of methods for accessing settings and flags.
u = User.new
u.settings.language = 'en/us'
u.flags.is_admin = true

If you have rails 2 or rails 3, check out Ledermann's rails-settings gem which has a syntax to get all key-value pairs just like you asked for:
son.settings.all
Easy to add settings:
son.settings.key = value
You also get activerecord scopes to search based on settings. And you can set default settings and global (application scope) settings.
For rails 2, you need to use version "~> 1.x" and follow docs: https://github.com/ledermann/rails-settings/blob/c5c34faf1bbe5742b58f6b3acff3874edc6e4bbc/README.md
If you need :before and :after event handling, check https://github.com/exceed/preferential/

Related

How can Devise users create an invitation for others to accept and start and able to finish invitation

I'm creating an app in rails that has User's and that has Dogwalkers, these users are created on Devise. So, I wan't that a User is able to create a Dog walk for and set, date, time and add the dogs that are going to take for a walk.
I got my Dog's Database on Sqlite:
create_table :dogs do |t|
t.string :name
t.string :photo
t.string :race
t.references :user, foreign_key: true
t.timestamps
end
add_index :dogs, [:user_id, :created_at]
And I got my Walks model:
class CreateWalks < ActiveRecord::Migration[5.2]
def change
create_table :walks do |t|
t.integer :walk_id
t.date :walk_at
t.datetime :walk_start_at
t.datetime :walk_end_at
t.integer :status
t.references :dog, foreign_key: true
t.references :user, foreign_key: true
t.references :dogwalker, foreign_key: true
t.float :dogprice
t.integer :doglimit
t.timestamps
end
add_index :walks, [:user_id, :created_at]
end
end
So how can I make that the user is able to create a Walk, and let the Dog walker able to see it on it's own dashboard (which I already created). The Dog Walker will accept it and it will send a notification to the user that it has been accept it. Once the user has received there dog's the walk has finish and create a history of the walk.
What I been doing:
Created the databases that I show above.
Made the views
Made the forms for the user able to create a new Dog trip
Created the controllers for the walk
Hope this possible! Thanks for you help!!!
I think what you want to do is create a form for a new Walk. The user(dog owner) will fin the required fields, like date, walk_start_at and walk_end_at. Maybe some more, but that's up to you.
On the users dashboard(the dogwalker) you can list all walks that available by:
#walks = Walk.where(dogwalker: nil) This will give you all the walks that don't have a walker yet. For each of these walks you also add a form for walk that only needs a submit button. When the dogwalker hits the button it goes to the update action from the Walk. There you receive the params and you can set the dogwalker.
Now on the dogowner can see that the walk is accepted.

How to use custom version table instead of version table?

For a project I'm currently working on I need to implement object versioning. Unfortunately I need to keep a full history of each object, so a single table solution like Papertrail would quickly become un-manageable. So I am trying to have custom version table for each object using papertrail. I followed the documentation but getting error on creating object
Model::UnknownAttributeError: unknown attribute 'event' for ArticleVersion.
Here is the implemented code :
class Article < ActiveRecord::Base
has_paper_trail class_name: 'ArticleVersion'
end
class ArticleVersion < PaperTrail::Version
self.table_name = :article_versions
end
module PaperTrail
class Version < ActiveRecord::Base
include PaperTrail::VersionConcern
self.abstract_class = true
end
end
Here is the migration for Article migrations:
class CreateArticleVersions < ActiveRecord::Migration[5.0]
def change
create_table :article_versions do |t|
t.string :title
t.text :text
t.timestamps
end
end
end
and When I am trying to create article by Article.create(text:'some text')
I am getting the above mentioned error.
Can anyone help me to figure out if I am missing anything.
create_table :article_versions do |t|
t.string :title
t.text :text
t.timestamps
end
Model::UnknownAttributeError: unknown attribute 'event' for ArticleVersion.
The ArticleVersion model lacks an event attribute because the article_versions table lacks an event column.

using activeuuid gem for existing database in rails

I need to implement activeuuid gem to have UUIDs instead of default Rails ids. we can implement it for creating new migration as:
class CreateStudents < ActiveRecord::Migration
def change
create_table :students, :id => false do |t|
t.uuid :id, :primary_key => true
t.string :name
t.string :email
t.timestamps
end
end
end
And in model we include ActiveUUID::UUID as:
class Student < ActiveRecord::Base
attr_accessible :email, :name
include ActiveUUID::UUID
end
Now I already have a database so how can I implement the activeuuid gem to have UUIDs instead of default Rails ids for existing DB?
Need to make changes in all migrations or what?
Need help in this regard. thanks
The UUID is stored as a binary field w/ 16 positions as I found here: https://github.com/jashmenn/activeuuid/blob/master/lib/activeuuid/patches.rb#L62
It worked for me (existing table without records):
def change
reversible do |dir|
change_table :payments do |t|
dir.up { t.change :id, :binary, limit: 16, :primary_key => true }
dir.down { t.change :id, :integer }
end
end
end
Don't forget to add those lines to your model as well:
include ActiveUUID::UUID
natural_key :at_least_one_field_here
More info in the github repo: https://github.com/jashmenn/activeuuid/

cant mass assign the protected attributes

iam working in rails 3.while trying to creating a user i am getting
cant mass assign the protected attributes error
I included following gems in the gemfile
gem 'authlogic'
gem 'gemcutter'
and run bundle install in rails console
then create a a user model and add the required authlogic columns to the migration.
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :login, :null => false
t.string :crypted_password, :null => false
t.string :password_salt, :null => false
t.string :persistence_token, :null => false
t.timestamps
end
end
end
and did rake db:migrate
Included authlogic in the user model.
# /app/models/user.rb
class User < ActiveRecord::Base
acts_as_authentic
end
while trying to create a user in rails console User.create(name: "pria",password: "priya123", password_confirmation: "priya123")
iam getting
cant mass assign the protected attributes :name, :password, :password_confirmation
How can i rectify this error!
In your User model:
attr_accessible :name, :password, :password_confirmation
You must add these attributes to the attr_accessible list in your model.
For important information about mass-assignment and its security implications: http://guides.rubyonrails.org/security.html#mass-assignment

passing object for polymorphic lookup parameter in Rails find/where

Let's say I have:
class Comment < ActiveRecord::Base
belongs_to :commentable, :polymorphic => true
end
class Article < ActiveRecord::Base
has_many :comments, :as => :commentable
end
class Photo < ActiveRecord::Base
has_many :comments, :as => :commentable
#...
end
now I want to find all comments on Jim's photo:
#jims_photo = Photo.where(:of => "Jim")
#photo_comments = Comment.where(:commentable => #jims_photo)
this seems to not work in rails (Rails 3). The generated query seems to not expand the polymorphic object into commentable_id and commentable_type fields:
SQLite3::SQLException: no such column: comments.commentable:
I'm new to ruby and rails so I might be using the paradigm incorrectly but my expectation was that rails automatically expands
:commentable => #jims_photo
to:
:commentable_id => #jims_photo.id, :commentable_type => #jims_photo.class.name
If you want to be really safe with:
:commentable_id => #jims_photo.id, :commentable_type => #jims_photo.class.name
Then I'd recommend replacing .class.name with .base_class (you don't actually need the name: to_s returns name and will be called automatically).
The reason for this is that when ActiveRecord saves the _type for a polymorphic association it'll use base_class to ensure it's not saving a class which itself is a polymorphic one.
If you play with store_full_sti_class you'll probably have to take even more precautions.
I highly recommend looking at Rails' STI code, here.
The guides for Rails are one of the best so I'd suggest you start reading about Polymorphic Associations
You class declarations looks OK and I'm assuming that you're migrations is as well. But just for the sake of it. Let's say it looks like this:
class CreateComment < ActiveRecord::Migration
def change
create_table :comments do |t|
t.string :name
t.references :commentable, :polymorphic => true
# this is the equivalent of
# t.integer :commentable_id
# t.string :commentable_type
t.timestamps
end
end
end
Not if you have a Article or Photo object and you want to get the comments for that object then Thilo's suggestion is right on. All you need to do is this: #jims_photo.comments
If, on the other hand, you have a an instance of the Comment model, you can get the parent like this: #comment.commentable. But if you want to get Jim's photo comments best to do it like that. Otherwise, you'd have to supply as arguments both the :commentable_id and commentable_type. I'm not aware of any finder that expands the polymorphic object into commentable_id and commentable_type fields for you.
But you can always create a class method for that:
def self.find_by_parent(parent)
where(:commentable_id => parent.id, :commentable_type => parent.class.name)
end