Commit 5d4a5f51 by John Doe

added visibility property to dataviews

parent ece8d62d
......@@ -38,11 +38,11 @@ class DataViewsController < ApplicationController
private
def data_view_params
params.permit(:name, :description, :image, :redux_json)
params.permit(:name, :description, :visibility, :image, :redux_json)
end
def updatable_data_view_params
params.permit(:name, :description)
params.permit(:name, :description, :visibility)
end
def set_data_view
......
......@@ -10,6 +10,9 @@ class DataView < ApplicationRecord
#---Validations-----
validates :name, :presence => true
TYPES = %w(public private hidden)
validates :visibility, :inclusion => {:in => TYPES}
#return all DataViews that can be loaded by this user
def self.find_viewable(user)
......@@ -18,14 +21,20 @@ class DataView < ApplicationRecord
nilms = r[:admin]+r[:owner]+r[:viewer]
#find all DataViewsNilms that are not allowed
prohibited = DataViewsNilm.where.not(nilm_id: nilms.pluck(:id))
#find the *other* DataViewsNilms (allowed)
allowed_ids = DataViewsNilm.where.not(id: prohibited.pluck(:id))
.pluck(:data_view_id).uniq
DataView.find(allowed_ids)
#find all visible data views
visible_ids = DataView.where(owner: user).
or(DataView.where(visibility: 'public')).
pluck(:id)
#permitted views must be both visible and allowed
DataView.where(id: allowed_ids&visible_ids)
end
def self.json_keys
[:id, :name, :description, :image, :redux_json]
[:id, :name, :description, :image, :redux_json, :visibility]
end
end
......@@ -69,6 +69,7 @@ class LoadStreamData
@data = __build_decimated_data(decimateable_elements, resp) +
__build_intervals_from_decimated_data(interval_elements, resp)
end
self
end
#===Description
......
......@@ -9,23 +9,28 @@
# total time: <%=distance_of_time_in_words(@legend[:end_time]/1e6-@legend[:start_time]/1e6)%>
# total rows: <%=@legend[:num_rows]%>
# decimation factor: <%=@legend[:decimation_factor]%>
# notes: <%=@legend[:notes]%>
# legend:
<%@legend[:columns].each do |col|%>
# Column <%=col[:index]%>: <%=col[:name]%> (<%=col[:units]%>)
<%end%>
#
# --------- MATLAB INSTRUCTIONS ------------
#
# this file can be loaded directly into MATLAB
#
# dataset = importdata('thisfilename.csv')
# >> x = importdata('~/Downloads/filename.csv')
# x =
# data: [1737x15 double] % the data
# textdata: {41x1 cell} % this help text
#
# dataset.textdata: this help text
# dataset.data: the data
# --------- NILMTOOL INSTRUCTIONS ----------
#
# raw data can be accessed using nilmtool, run:
# nilmtool -u <%=@db.url%> extract -s @<%=@legend[:start_time]%> -e @<%=@legend[:end_time]%> <%=@db_stream.path%>
#
# <%=@legend[:notes]%>
# ----------- LEGEND ---------------
<%@legend[:columns].each do |col|%>
# Column <%=col[:index]%>: <%=col[:name]%> (<%=col[:units]%>)
<%end%>
# -----------------------------------
# $> nilmtool -u <%=@db.url%> extract -s @<%=@legend[:start_time]%> -e @<%=@legend[:end_time]%> <%=@db_stream.path%>
#
# ------------------------------------------
#
<% @data.each do |row|%>
<%= row.join(", ")%>
......
class AddVisibilityToDataViews < ActiveRecord::Migration[5.1]
def change
add_column :data_views, :visibility, :string
end
end
......@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20170512013633) do
ActiveRecord::Schema.define(version: 20170524005258) do
create_table "data_views", force: :cascade do |t|
t.integer "user_id"
......@@ -20,6 +20,7 @@ ActiveRecord::Schema.define(version: 20170512013633) do
t.text "redux_json"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "visibility"
end
create_table "data_views_nilms", force: :cascade do |t|
......
......@@ -17,25 +17,24 @@ RSpec.describe DataViewsController, type: :request do
other_stream = create(:db_stream, db: other_db)
other_user = create(:user)
service = CreateDataView.new
allowed_view = service.run(
{name: 'allowed'}, [viewed_streams.first.id], other_user)
prohibited_view = service.run(
{name: 'prohibited'}, [other_stream.id], other_user)
my_view = service.run(
{name: 'created'}, viewed_streams.map{|x| x.id}, viewer)
service.run(
{name: 'allowed', visibility: 'public'}, [viewed_streams.first.id], other_user)
service.run(
{name: 'prohibited', visibility: 'public'}, [other_stream.id], other_user)
service.run(
{name: 'private', visibility: 'private'}, [viewed_streams.first.id], other_user)
service.run(
{name: 'my_public', visibility: 'public'}, viewed_streams.map{|x| x.id}, viewer)
service.run(
{name: 'my_private', visibility: 'private'}, viewed_streams.map{|x| x.id}, viewer)
#viewer should receive 'allowed' and 'created'
@auth_headers = viewer.create_new_auth_token
get "/data_views.json", headers: @auth_headers
expect(response).to have_http_status(:ok)
body = JSON.parse(response.body)
expect(body.length).to eq 2
body.each do |view|
if view['name']=='created'
expect(view['owner']).to be true
else
expect(view['owner']).to be false
end
end
names = body.map {|view| view['name']}
expect(names).to contain_exactly('allowed','my_public','my_private')
end
end
context 'without sign-in' do
......@@ -53,7 +52,7 @@ RSpec.describe DataViewsController, type: :request do
post "/data_views.json",
params: {
name: 'test', description: '', image: '', redux_json: '',
stream_ids: viewed_streams.map {|x| x.id}
visibility: 'public', stream_ids: viewed_streams.map {|x| x.id}
}, headers: @auth_headers
expect(response).to have_http_status(:ok)
expect(response).to have_notice_message
......@@ -66,7 +65,7 @@ RSpec.describe DataViewsController, type: :request do
post "/data_views.json",
params: {
description: 'missing name', image: '', redux_json: '',
stream_ids: viewed_streams.map {|x| x.id}
visibility: 'public', stream_ids: viewed_streams.map {|x| x.id}
}, headers: @auth_headers
expect(response).to have_http_status(:unprocessable_entity)
expect(response).to have_error_message
......@@ -84,7 +83,7 @@ RSpec.describe DataViewsController, type: :request do
before do
service = CreateDataView.new
service.run(
{name: 'created'}, viewed_streams.map{|x| x.id}, viewer)
{name: 'created', visibility: 'public'}, viewed_streams.map{|x| x.id}, viewer)
@my_view = service.data_view
end
context 'with view owner' do
......
......@@ -2,6 +2,7 @@ FactoryGirl.define do
factory :data_view do
name { Faker::Lorem.words(3).join(' ') }
description { Faker::Lorem.sentence }
visibility "public"
redux_json "auto generated from factory"
end
end
......@@ -8,6 +8,7 @@ RSpec.describe DataView, type: :model do
specify { expect(data_view).to respond_to(:redux_json) }
specify { expect(data_view).to respond_to(:owner) }
specify { expect(data_view).to respond_to(:nilms) }
specify { expect(data_view).to respond_to(:visibility) }
it 'is deleted when nilm is destroyed' do
nilm1 = create(:nilm)
......
......@@ -13,6 +13,7 @@ describe 'CreateDataView service' do
it 'creates a dataview' do
params = {
name: 'test',
visibility: 'public',
description: '',
image: '',
redux_json: ''}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment