publish_my_data 0.0.28 → 0.0.29

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -36,9 +36,8 @@ module PublishMyData
36
36
 
37
37
  # /data?page=2&per_page=10
38
38
  def index
39
- dataset_criteria = Dataset.ordered_datasets_criteria
40
39
  @pagination_params = ResourcePaginationParams.from_request(request)
41
- @datasets = Paginator.new(dataset_criteria, @pagination_params).paginate
40
+ @datasets = Paginator.new(Dataset.deprecation_last_query_str, @pagination_params, resource_class: PublishMyData::Dataset).paginate
42
41
  respond_with(@datasets)
43
42
  end
44
43
 
@@ -18,6 +18,7 @@ module PublishMyData
18
18
 
19
19
  @pagination_params = ResourcePaginationParams.from_request(request)
20
20
  @resources = Paginator.new(resource_criteria, @pagination_params).paginate
21
+
21
22
  respond_with(@resources)
22
23
  end
23
24
 
@@ -16,7 +16,7 @@ module PublishMyData
16
16
 
17
17
  if @theme
18
18
  @pagination_params = ResourcePaginationParams.from_request(request)
19
- @datasets = Paginator.new(@theme.datasets_criteria, @pagination_params).paginate
19
+ @datasets = Paginator.new(@theme.datasets_query_str, @pagination_params, resource_class: PublishMyData::Dataset).paginate
20
20
  respond_with(@datasets)
21
21
  else
22
22
  raise Tripod::Errors::ResourceNotFound
@@ -103,6 +103,33 @@ module PublishMyData
103
103
  def ordered_datasets_criteria
104
104
  Dataset.all.where("?uri <#{RDF::DC.title}> ?title").order("?title")
105
105
  end
106
+
107
+ def deprecation_last_query_str
108
+ "
109
+ SELECT ?uri where {
110
+ # this bit is all the non-deprecated ones
111
+ {
112
+ SELECT * WHERE {
113
+ ?uri a <http://publishmydata.com/def/dataset#Dataset> .
114
+ ?uri <#{RDF::DC.title}> ?title . # select title so we can order
115
+ MINUS {
116
+ ?uri a <http://publishmydata.com/def/dataset#DeprecatedDataset>
117
+ }
118
+ }
119
+ ORDER BY ?title
120
+ }
121
+ UNION
122
+ # this bit is all the deprecated ones
123
+ {
124
+ SELECT * WHERE {
125
+ ?uri a <http://publishmydata.com/def/dataset#DeprecatedDataset> .
126
+ ?uri <#{RDF::DC.title}> ?title . # select title so we can order
127
+ }
128
+ ORDER BY ?title
129
+ }
130
+ }
131
+ "
132
+ end
106
133
  end
107
134
  end
108
135
  end
@@ -22,10 +22,38 @@ module PublishMyData
22
22
  field :slug, RDF::SKOS.notation
23
23
  field :comment, RDF::RDFS.comment
24
24
 
25
- def datasets_criteria
26
- Dataset
27
- .ordered_datasets_criteria
28
- .where("?uri <#{RDF::DCAT.theme}> <#{self.uri.to_s}>")
25
+ def datasets_count
26
+ PublishMyData::SparqlQuery.new(datasets_query_str).count
27
+ end
28
+
29
+ def datasets_query_str
30
+ # this is similar to the deprecation_last_query_str, but with a theme restriction
31
+ "
32
+ SELECT ?uri where {
33
+ # this bit is all the non-deprecated ones
34
+ {
35
+ SELECT * WHERE {
36
+ ?uri a <http://publishmydata.com/def/dataset#Dataset> .
37
+ ?uri <#{RDF::DCAT.theme}> <#{self.uri.to_s}> . # limit to this theme
38
+ ?uri <#{RDF::DC.title}> ?title . # select title so we can order
39
+ MINUS {
40
+ ?uri a <http://publishmydata.com/def/dataset#DeprecatedDataset>
41
+ }
42
+ }
43
+ ORDER BY ?title
44
+ }
45
+ UNION
46
+ # this bit is all the deprecated ones
47
+ {
48
+ SELECT * WHERE {
49
+ ?uri a <http://publishmydata.com/def/dataset#DeprecatedDataset> .
50
+ ?uri <#{RDF::DCAT.theme}> <#{self.uri.to_s}> . # limit to this theme
51
+ ?uri <#{RDF::DC.title}> ?title . # select title so we can order
52
+ }
53
+ ORDER BY ?title
54
+ }
55
+ }
56
+ "
29
57
  end
30
58
 
31
59
  def to_param
@@ -1,7 +1,7 @@
1
1
  <h1>Themes</h1>
2
2
 
3
3
  <% @themes.each do |t| %>
4
- <%= link_to (t.label || t.uri), theme_path(t) %> (<%= pluralize( t.datasets_criteria.count, "dataset" )%>)
4
+ <%= link_to (t.label || t.uri), theme_path(t) %> (<%= pluralize( t.datasets_count, "dataset" )%>)
5
5
  <%= t.comment %>
6
6
  <% end %>
7
7
 
@@ -49,38 +49,55 @@ module PublishMyData
49
49
  class Paginator
50
50
 
51
51
  attr_accessor :pagination_params
52
- attr_accessor :criteria
52
+ attr_accessor :resource_class
53
+ attr_accessor :sparql_query # PublishMyData::SparqlQuery
54
+
55
+ # criteria can be a Tripod::Criteria or a sparql string.
56
+ # pagination_params should be an instance pagination params.
57
+ # if criteria is a sparql string, optionally pass options[:resource_class] to dictate what type of objects to return (else it will return Resources)
58
+ def initialize(criteria, pagination_params, opts={})
59
+
60
+ if criteria.class == String
61
+ self.sparql_query = PublishMyData::SparqlQuery.new(criteria)
62
+ self.resource_class = opts[:resource_class] || PublishMyData::Resource
63
+ elsif criteria.class == Tripod::Criteria
64
+ # Note that this uses the :return_graph => false option for criteria execution to avoid duplicate graphs in the results
65
+ self.sparql_query = PublishMyData::SparqlQuery.new(criteria.as_query(:return_graph => false))
66
+ self.resource_class = criteria.resource_class
67
+ end
53
68
 
54
- def initialize(criteria, pagination_params)
55
- self.criteria = criteria
56
69
  self.pagination_params = pagination_params
57
70
  end
58
71
 
59
- # returns a Kaminari paginatable array, or a plain old array
60
- # Note that this uses the :return_graph => false option for criteria execution to avoid duplicate graphs in the results
61
- # (which could mess with the pagination counts)
62
- # optionally pass an integer to use for the total count.
63
- def paginate(total_count=nil)
64
- if self.pagination_params.format == :html && pagination_params.per_page && pagination_params.page
65
- count = total_count || criteria.count #this has to happen first, before we modify the criteria with limit/offset
66
- add_limit_and_offset_criteria(criteria)
67
- paginatable = Kaminari.paginate_array(criteria.resources(:return_graph => false).to_a, total_count: count).page(self.pagination_params.page).per(self.pagination_params.per_page)
72
+ # returns a Kaminari paginatable array (for html), or a plain old array (for data formats)
73
+ def paginate(force_total_count=nil)
74
+
75
+ page = self.pagination_params.page
76
+ per_page = self.pagination_params.per_page
77
+ pagination_query_str = self.sparql_query.as_pagination_query(page, per_page).query
78
+
79
+ if self.pagination_params.format == :html
80
+ total_count = force_total_count || self.sparql_query.count
81
+ page_of_results = resource_class.find_by_sparql(pagination_query_str)
82
+ Kaminari.paginate_array(page_of_results, total_count: total_count).page(page).per(per_page)
68
83
  else
69
- add_limit_and_offset_criteria(criteria)
70
- criteria.resources(:return_graph => false) # non html versions just need the raw array
84
+ page_of_results = resource_class.find_by_sparql(pagination_query_str)
85
+
86
+ Tripod::ResourceCollection.new(
87
+ page_of_results,
88
+ :return_graph => false,
89
+ :sparql_query_str => pagination_query_str,
90
+ :resource_class => self.resource_class
91
+ )
92
+
71
93
  end
94
+
72
95
  end
73
96
 
74
97
  def ==(other)
75
98
  self.pagination_params == other.pagination_params &&
76
- self.criteria == other.criteria
77
- end
78
-
79
- private
80
-
81
- def add_limit_and_offset_criteria(criteria)
82
- criteria.limit(self.pagination_params.per_page) if self.pagination_params.per_page
83
- criteria.offset(self.pagination_params.offset) if self.pagination_params.offset
99
+ self.sparql_query == other.sparql_query &&
100
+ self.resource_class == self.resource_class
84
101
  end
85
102
 
86
103
  end
@@ -1,7 +1,6 @@
1
1
  require "publish_my_data/render_params/dataset_render_params"
2
2
  require "publish_my_data/render_params/resource_render_params"
3
3
  require "publish_my_data/render_params/ontology_render_params"
4
- require "publish_my_data/render_params/theme_render_params"
5
4
  require "publish_my_data/render_params/property_render_params"
6
5
  require "publish_my_data/render_params/ontology_class_render_params"
7
6
  require "publish_my_data/render_params/concept_scheme_render_params"
@@ -91,7 +91,6 @@ module PublishMyData
91
91
  end
92
92
 
93
93
  def as_count_query(format = :json)
94
- # return the paginated version
95
94
  PublishMyData::SparqlQuery.new(as_count_query_str, {:request_format => format, :parent_query => self}) # pass in the original query
96
95
  end
97
96
 
@@ -1,3 +1,3 @@
1
1
  module PublishMyData
2
- VERSION = "0.0.28"
2
+ VERSION = "0.0.29"
3
3
  end
@@ -20,7 +20,18 @@ module PublishMyData
20
20
  shared_examples_for "a dataset collection in non-html" do
21
21
  it "should render the collection in the right format" do
22
22
  get :index, :page => page, :per_page => per_page, :format => format, use_route: :publish_my_data
23
- response.body.should == Dataset.ordered_datasets_criteria.limit(per_page).offset(offset).resources.send("to_#{format}")
23
+
24
+ q = SparqlQuery.new(Dataset.deprecation_last_query_str).as_pagination_query(page, per_page).query
25
+ datasets_array = Dataset.find_by_sparql(q).to_a
26
+
27
+ collection = Tripod::ResourceCollection.new(
28
+ datasets_array,
29
+ :return_graph => false,
30
+ :sparql_query_str => q,
31
+ :resource_class => Dataset
32
+ )
33
+
34
+ response.body.should == collection.send("to_#{format}")
24
35
  end
25
36
 
26
37
  it "shouldn't call Kaminari" do
@@ -36,21 +47,26 @@ module PublishMyData
36
47
 
37
48
  shared_examples_for "dataset kaminari pagination" do
38
49
  it "should call kaminari to paginate the results" do
39
- datasets_array = Dataset.ordered_datasets_criteria.limit(per_page).offset(offset).resources.to_a
50
+ q = SparqlQuery.new(Dataset.deprecation_last_query_str).as_pagination_query(page, per_page).query
51
+ datasets_array = Dataset.find_by_sparql(q).to_a
40
52
  count = Dataset.count
41
53
 
42
54
  kam = Kaminari.paginate_array(datasets_array, total_count: count)
43
-
44
55
  Kaminari.should_receive(:paginate_array).with(datasets_array, total_count: count).and_return(kam)
45
56
  kam.should_receive(:page).with(page).and_return(kam)
46
57
  kam.should_receive(:per).with(per_page).and_return(kam)
58
+
47
59
  get :index, page: page, per_page: per_page, use_route: :publish_my_data
48
60
  end
49
61
 
50
62
  it "should set @datasets with the right page of datasets" do
51
63
  get :index, page: page, per_page: per_page, use_route: :publish_my_data
64
+
65
+ datasets = Dataset.find_by_sparql(Dataset.deprecation_last_query_str)
66
+
52
67
  assigns['datasets'].map{ |d| d.uri.to_s }.should ==
53
- Dataset.ordered_datasets_criteria.resources[offset...offset+per_page].map{ |d| d.uri.to_s }
68
+ datasets[offset...offset+per_page].map{ |d| d.uri.to_s }
69
+
54
70
  assigns['datasets'].length.should == per_page
55
71
  end
56
72
 
@@ -62,10 +78,7 @@ module PublishMyData
62
78
  let(:offset) { (page-1)*per_page }
63
79
 
64
80
  it "should retreive the first page of results" do
65
- crit = Dataset.ordered_datasets_criteria
66
- Dataset.should_receive(:ordered_datasets_criteria).at_least(:once).and_return(crit)
67
- crit.should_receive(:limit).with(per_page).and_call_original
68
- crit.should_receive(:offset).with(offset).and_call_original
81
+ PublishMyData::SparqlQuery.any_instance.should_receive(:as_pagination_query).with(page, per_page)
69
82
  get :index, use_route: :publish_my_data
70
83
  end
71
84
 
@@ -85,10 +98,7 @@ module PublishMyData
85
98
  let(:offset) { (page-1)*per_page }
86
99
 
87
100
  it "should retreive the right page of results" do
88
- crit = Dataset.ordered_datasets_criteria
89
- Dataset.should_receive(:ordered_datasets_criteria).at_least(:once).and_return(crit)
90
- crit.should_receive(:limit).with(per_page).and_call_original
91
- crit.should_receive(:offset).with(offset).and_call_original
101
+ PublishMyData::SparqlQuery.any_instance.should_receive(:as_pagination_query).with(page, per_page)
92
102
  get :index, page: page, per_page: per_page, use_route: :publish_my_data
93
103
  end
94
104
 
@@ -286,6 +286,7 @@ module PublishMyData
286
286
  shared_examples_for "a resource collection in non-html" do
287
287
  it "should render the collection in the right format" do
288
288
  get :index, :page => page, :per_page => per_page, :format => format, use_route: :publish_my_data
289
+ puts format
289
290
  response.body.should == Resource.all.limit(per_page).offset(offset).resources.send("to_#{format}")
290
291
  end
291
292
 
@@ -350,10 +351,7 @@ module PublishMyData
350
351
  let(:offset) { (page-1)*per_page }
351
352
 
352
353
  it "should retreive the right page of results" do
353
- crit = Resource.all
354
- Resource.should_receive(:all).at_least(:once).and_return(crit)
355
- crit.should_receive(:limit).with(per_page).and_call_original
356
- crit.should_receive(:offset).with(offset).and_call_original
354
+ PublishMyData::SparqlQuery.any_instance.should_receive(:as_pagination_query).with(page, per_page)
357
355
  get :index, page: page, per_page: per_page, use_route: :publish_my_data
358
356
  end
359
357
 
@@ -53,7 +53,7 @@ module PublishMyData
53
53
  it "should respond with the paginated collection of datasets in that theme" do
54
54
  get :show, :id => theme.slug, :use_route => :publish_my_data, :format => 'ttl', :per_page => 5
55
55
  pagination_params = ResourcePaginationParams.from_request(@request)
56
- paginator = Paginator.new(theme.datasets_criteria, ResourcePaginationParams.from_request(@request))
56
+ paginator = Paginator.new(theme.datasets_query_str, ResourcePaginationParams.from_request(@request), resource_class: PublishMyData::Dataset)
57
57
  paginator.paginate.length.should == 5 #just the page's worth
58
58
  response.body.should == paginator.paginate.to_ttl
59
59
  end