Module: Backframe::ActsAsAPI::Page
- Defined in:
- lib/backframe/actioncontroller/acts_as_api/page.rb
Constant Summary collapse
- DEFAULT_PAGE =
1- DEFAULT_PER_PAGE =
100
Instance Method Summary collapse
- #collection_to_array(collection, serializer, fields) ⇒ Object
- #collection_to_csv(collection, serializer, fields, separator) ⇒ Object
- #collection_to_xls(collection, serializer, fields) ⇒ Object
- #expand_fields(collection, serializer, fields) ⇒ Object
- #flatten_hash_keys(hash, prefix = '') ⇒ Object
- #page(collection, serializer = nil) ⇒ Object
- #pagination_link(per_page, page) ⇒ Object
- #pagination_links(collection, per_page, page) ⇒ Object
Instance Method Details
#collection_to_array(collection, serializer, fields) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/backframe/actioncontroller/acts_as_api/page.rb', line 90 def collection_to_array(collection, serializer, fields) rows = [] cols = [] row = [] fields.each do |key| row << key end rows << row collection.all.each do |record| row = [] serialized = serializer.new(record).attributes fields.each_with_index do |fullkey, col| value = serialized fullkey.to_s.split(".").each do |key| key = key.to_sym if value.is_a?(Hash) && value.key?(key) value = value[key] else value = nil end end if value.is_a?(Time) value = value.strftime("%F %T") elsif value.is_a?(Date) value = value.strftime("%F") elsif value.is_a?(TrueClass) value = 'true' elsif value.is_a?(FalseClass) value = 'false' elsif value.is_a?(NilClass) value = '' end row << "#{value}" end rows << row end rows end |
#collection_to_csv(collection, serializer, fields, separator) ⇒ Object
129 130 131 132 133 134 135 136 |
# File 'lib/backframe/actioncontroller/acts_as_api/page.rb', line 129 def collection_to_csv(collection, serializer, fields, separator) output = [] rows = collection_to_array(collection, serializer, fields) rows.each do |row| output << row.join(separator) end output.join("\n") end |
#collection_to_xls(collection, serializer, fields) ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/backframe/actioncontroller/acts_as_api/page.rb', line 138 def collection_to_xls(collection, serializer, fields) filename = SecureRandom.hex(32).to_s.upcase[0,16] workbook = WriteXLSX.new(filename) worksheet = workbook.add_worksheet rows = collection_to_array(collection, serializer, fields) rows.each_with_index do |row, i| row.each_with_index do |col, j| worksheet.write(i, j, col) end end workbook.close data = open(filename).read File.unlink(filename) data end |
#expand_fields(collection, serializer, fields) ⇒ Object
68 69 70 71 72 73 74 75 |
# File 'lib/backframe/actioncontroller/acts_as_api/page.rb', line 68 def (collection, serializer, fields) if fields.present? fields.split(',').map(&:to_s) else serialized = serializer.new(collection.first).attributes flatten_hash_keys(serialized) end end |
#flatten_hash_keys(hash, prefix = '') ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/backframe/actioncontroller/acts_as_api/page.rb', line 77 def flatten_hash_keys(hash, prefix = '') keys = [] hash.each do |key, value| fullkey = (!prefix.empty?) ? "#{prefix}.#{key}" : key if value.is_a?(Hash) keys.concat(flatten_hash_keys(value, fullkey)) else keys << fullkey.to_s end end keys end |
#page(collection, serializer = nil) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/backframe/actioncontroller/acts_as_api/page.rb', line 7 def page(collection, serializer = nil) classname = collection.base_class.name+'Serializer' serializer ||= classname.constantize args = params.except(*request.path_parameters.keys) filters = args.except([:sort,:page,:per_page,:fields,:exclude_ids]) model = (collection.respond_to?(:klass)) ? collection.klass.name.constantize : collection collection = model.filter(collection, filters) if collection.respond_to?(:filter) if args.key?(:sort) args[:sort].split(',').each do |sort| key = (sort[0] == '-') ? sort[1..-1] : sort order = (sort[0] == '-') ? 'desc' : 'asc' collection = model.sort(collection, key, order) end else collection = model.sort(collection) end if args.key?(:exclude_ids) ids = args[:exclude_ids].split(',') collection = collection.where('id NOT IN (?)', ids) end if args.key?(:all) args[:page] = 1 args[:per_page] = 10000 end args[:page] ||= DEFAULT_PAGE args[:per_page] ||= DEFAULT_PER_PAGE collection = (params[:format] == 'json') ? collection.page(args[:page]).per(args[:per_page]) : collection.all respond_to do |format| format.json { fields = (args.key?(:fields)) ? args[:fields].split(',').map(&:to_sym) : serializer._attributes render json: collection, each_serializer: serializer, content_type: 'application/json', adapter: Backframe::ActsAsAPI::Adapter, fields: fields, links: pagination_links(collection, args[:per_page], args[:page]), status: 200 } format.csv { fields = (collection, serializer, args[:fields]) content_type = (args.key?(:download) && args[:download] == 'false') ? 'text/plain' : 'text/csv' render :text => collection_to_csv(collection, serializer, fields, ","), :content_type => content_type, :status => 200 } format.tsv { fields = (collection, serializer, args[:fields]) content_type = (args.key?(:download) && args[:download] == 'false') ? 'text/plain' : 'text/tab-separated-values' render :text => collection_to_csv(collection, serializer, fields, "\t"), :content_type => content_type, :status => 200 } format.xlsx { fields = (collection, serializer, args[:fields]) render :text => collection_to_xls(collection, serializer, fields), :content_type => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', :status => 200 } end end |
#pagination_link(per_page, page) ⇒ Object
173 174 175 176 |
# File 'lib/backframe/actioncontroller/acts_as_api/page.rb', line 173 def pagination_link(per_page, page) args = (per_page.to_i != DEFAULT_PER_PAGE) ? "per_page="+per_page.to_s+"&page="+page.to_s : "page="+page.to_s base_api_url+request.path+"?"+args end |
#pagination_links(collection, per_page, page) ⇒ Object
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/backframe/actioncontroller/acts_as_api/page.rb', line 154 def pagination_links(collection, per_page, page) return {} if collection.total_count.zero? links = {} links[:self] = pagination_link(per_page, page) if collection.next_page.present? links[:next] = pagination_link(per_page, collection.next_page) end if page.to_i < collection.total_pages links[:last] = pagination_link(per_page, collection.total_pages) end if page.to_i > 1 links[:first] = pagination_link(per_page, 1) end if collection.prev_page.present? links[:prev] = pagination_link(per_page, collection.prev_page) end links end |