Mongo object's id as uploadings dirname in CarrierWave - ruby

I'm using Sinatra with Mongoid and CarrierWave. I need to store document's attachments in /public/attachments/DOCUMENTS_ID.
Model of Mongo document:
class Dcmnt
include Mongoid::Document
store_in collection: 'dcmnts'
field :published, type: Boolean
field :name, type: String
field :description, type: String
field :additional, type: String
field :created_at, type: Date
mount_uploader :attachment, Uploader, type: String
end
And action's code:
post '/admin/create' do
params.delete 'submit'
d = Dcmnt.new(
:published => params[:published],
:name => params[:name],
:description => params[:description],
:additional => params[:additional],
:created_at => Time.now
)
d.attachment = params[:photos]
d.save
end
When I'm setting up unloader like this:
class Uploader < CarrierWave::Uploader::Base
storage :file
def store_dir
'public/attachments/' + d.id
end
end
It doesn't works for some amzaing reason. Can you help me implement this feature?

Accessing models attributes in CarrierWave is provided via model key word
class Uploader < CarrierWave::Uploader::Base
storage :file
def store_dir
'attachments/' + model.id
end
end

Related

Neo4j and Ruby/Rails: How to only return nodes based on user permissions

How to return an index of only Items where the User has permission?
How to return an index of only Items where the Group a User is in has permission?
How to return a single item only if a User has permission?
How to return a single item only if the Group a User is in has permission?
Note: I know it's possible to query for Item.all, then iterate through the array and pull out only items where .has_permissions == User, but this completely ignores the benefits of having everything in a graph, so is not an answer.
To keep this simple, let's say there are 3 objects:
An Item
A User
A Group
Typical graph situations:
(User)<-[:HAS_PERMISSIONS]-(Item)
(Group)<-[:HAS_PERMISSIONS]-(Item)
(Group)-[:HAS_MEMBERS]->(User)
With the models:
class Item
include Neo4j::ActiveNode
property :name, type: String
property :description, type: String
has_many :out, :user_permission_to, type: :PERMISSION_TO, model_class: :User
has_many :out, :group_permission_to, type: :PERMISSION_TO, model_class: :Group
end
class Identity
include Neo4j::ActiveNode
property :username, type: String
has_many :in, :permission_to, type: :PERMISSION_TO, model_class: :Item
has_many :in, :groups, type: :GROUP_MEMBER, model_class: :Group
end
class Group
include Neo4j::ActiveNode
property :group_name, type: String
has_many :in, :permission_to, type: :PERMISSION_TO, model_class: :Item
has_many :out, :members, type: :GROUP_MEMBER, model_class: :User
end
And with the simple controller:
# GET /items
def index
#items = Item.all
render json: #items
end
# GET /item/1
def show
render json: #item
end
For starters, I'd suggest checking out this article (the second half covers access control which is very similar)
"How to return an index of only Items where the User has permission?"
You could do this a couple of ways. More explicitly:
identity.as(:id).query.match("(id)<-[PERMISSION_TO*1..2]-(item:Item)").pluck(:item)
Alternatively, I think that this would work:
identity.permission_to(rel_length: 1..2)
"How to return an index of only Items where the Group a User is in has permission?"
Simple:
identity.groups.permission_to
"How to return a single item only if a User has permission?"
For the two solutions above:
identity.as(:id).query.match("(id)<-[PERMISSION_TO*1..2]-(item:Item)").limit(1).pluck(:item)
# or
identity.permission_to(rel_length: 1..2).first
"How to return a single item only if the Group a User is in has permission?"
identity.groups.permission_to
Separately, some feedback:
Using the term "index" the way you're using it is a bit confusing because Neo4j has indexes which allow for performant querying of properties on labels.
I would probably make my models like this:
class Item
include Neo4j::ActiveNode
property :name, type: String
property :description, type: String
has_many :in, :users_with_permission, type: :CAN_ACCESS, model_class: :Identity
has_many :in, :groups_with_permission, type: :CAN_ACCESS, model_class: :Group
end
class Identity
include Neo4j::ActiveNode
property :username, type: String
has_many :out, :accessible_items, type: :CAN_ACCESS, model_class: :Item
has_many :out, :groups, type: :IN_GROUP # Don't need `model_class: :Group` here
end
class Group
include Neo4j::ActiveNode
property :group_name, type: String
has_many :out, :accessible_items, type: :CAN_ACCESS, model_class: :Item
has_many :in, :members, type: :IN_GROUP, model_class: :Identity
# You could also do:
# has_many :in, :members, model_class: :Identity, origin: :groups
end

ActiveRecord::AssociationTypeMismatch: User(#49785948) expected, got Hashie::Mash(#52717320)

I'm having a Grape api and I want to save nested datas.
I have a model like this :
class Vehicule < ActiveRecord::Base
before_validation :set_principal, :if =>:new_record?
belongs_to :user
accepts_nested_attributes_for :user
end
The grape api :
# app/controller/api/v1/vehicules.rb
resource :vehicules do
desc "Update a vehicule."
params do
optional :confort, type: Float, desc: ""
optional :user, type: Hash do
optional :name, type: String
end
end
put ':id' do
#authenticate! #todo
Vehicule.find(params[:id]).update(vehicule_params)
end
And here is my test file
test "PUT /api/v1/vehicules/1" do
put("/api/v1/vehicules/1", { 'confort' => 3.4, 'user' => {'name' => 'name changed'} }, :format => "json")
assert(200, last_response.status)
vehicule = Vehicule.find(1)
assert_equal('name changed', vehicule.user.name, "Le nom de l'utilisateur aurait dû changer")
end
The message is
1) Error: API::V1::VehiculesTest#test_PUT_/api/v1/vehicules/1: ActiveRecord::AssociationTypeMismatch: User(#49785948) expected, got Hashie::Mas h(#52717320)
app/controllers/api/v1/vehicules.rb:38:in `block (2 levels) in <class:Vehicu les>'
test/controllers/api/v1/vehicules_test.rb:18:in `block in <class:VehiculesTe st>'
What do I do wrong ? is it the way I'm sending the data or is it my structure/declaration ?
You have to use 'user_attributes' keyword in order to use nested attributes. So you have to change your params block like this:
params do
optional :confort, type: Float, desc: ""
optional :user_attributes, type: Hash do
optional :name, type: String
end
end
The test file have to change accordingly.

Mongo, Mongoid join data from different models in Ruby on Rails

I'm adapting a Ruby on Rails application to work with MongoDB instead of PostgreSQL. I have this 2 class:
module Cms
class Content
include Mongoid::Document
include Mongoid::Orderable
field :title, type: String
field :position, type: String
field :text_body, type: String
field :expiration_date, type: Date
field :active, type: Boolean, default: false
validates :title, :text_body, presence: true
orderable base: 0
belongs_to :cms_content_category, class_name: 'Cms::ContentCategory'
scope :ordered, ->() { order(position: :asc) }
scope :not_expired, ->() { any_of({ expiration_date: nil }, { :expiration_date.gte => Date.today }) }
end
end
module Cms
class Submenu
include Mongoid::Document
include Mongoid::Orderable
field :title, type: String
field :position, type: String
field :external_url, type: String
field :open_as, type: String, default: '_self'
field :active, type: Boolean, default: false
delegate :url_helpers, to: :'Rails.application.routes'
validates :title, presence: true
validates :external_url, presence: true, if: Proc.new { |obj| obj.cms_content_id.nil? }
validates :cms_content_id, presence: true, if: Proc.new { |obj| obj.external_url.blank? }
orderable base: 0
belongs_to :cms_content, class_name: 'Cms::Content'
belongs_to :cms_menu, class_name: 'Cms::Menu'
scope :ordered, ->() { order(position: :asc) }
def url
self.external_url.present? ? self.external_url : url_helpers.content_show_path(self.cms_content_id)
end
end
end
The problem is that I don't know how to adapt the following SQL query to work with MongoDB.
scope :active, joins("FULL JOIN contents ON contents.id = cms_submenus.content_id")
.where("cms_submenus.active IS TRUE AND (
(cms_submenus.content_id IS NOT NULL AND ((contents.expiration_date >= :today OR expiration_date IS NULL) AND contents.archived IS NOT TRUE)) OR
(cms_submenus.external_url IS NOT NULL AND cms_submenus.external_url <> '')
)", today: Date.today)
This scope has to be called from Submenu model.
Someone has an idea on how to solve this problem?
I'm using Rails 4.2.0 with Ruby 2.2.0 and Mongoid 4.0.2 with MongoDB 2.6.8.
thank you

Unable to create correct Mongoid model from JSON

I'm trying to create a Mongoid model from the corresponding JSON structure.
However it does not include the embedded relation frameworks.
I'm using Mongoid 4.0. Am I doing something wrong or is this a bug?
If I store any embedded relation via store_as under a different name than the default serialization, it works as expected. Also, if I create the model in the database from JSON rather than initialize it everything's fine...
JSON input
{
"name": "MyName",
"frameworks": [
{
"name": "grails",
"runtime": "groovy",
"versions": []
}
]
}
Models
require 'mongoid'
class Vendor
include Mongoid::Document
include Mongoid::Attributes::Dynamic
# fields
field :name, type: String
# relations
embeds_many :frameworks
# validations
validates :name, presence: true
validates :frameworks, presence: true
end
class Framework
include Mongoid::Document
embedded_in :vendor
field :name, type: String
field :runtime, type: String
field :versions, type: Array
# validations
validates :name, presence: true
validates :runtime, presence: true
end
Test App
require 'json'
require 'require_relative'
require_relative 'vendor'
begin
json = JSON.parse(File.read('input.json'))
#profile = Vendor.new(json)
puts #profile.inspect
rescue JSON::ParserError => e
puts "Error: " << e.to_s
end

Mongoid - getting all attributes including embedded documents

Is there an easy way to get all attributes of a Mongoid document, including those of embedded documents?
For example, if I have the following documents:
class Person
include Mongoid::Document
embeds_many :phone_numbers
field :name
end
class PhoneNumner
include Mongoid::Document
embedded_in :person, :inverse_of => :phone_numbers
field :number
end
I would like to get a Person's attributes and phone numbers like this:
{ :name => "Jenny", :phone_numbers => [{ :number => '867-5309' }, { :number => '867-5309' }] }
Since embedded documents are really just other attributes on the parent document, you can get to them like so:
person = Person.create
person.phone_numbers.create(:number => "123-456-7890")
person.attributes
# => {"_id"=>"4c48ff26f7e2da3704000001",
# "phone_numbers"=>
# [{"number"=>"123-456-7890", "_id"=>"4c48ff26f7e2da3704000002"}]}

Resources