theme_asset repository (both works with MongoDB and Filesystem adapters)
did
committed Feb 27, 2015
commit 1bdb5ed570b0479474278664e4f3bde825216f28
Showing 30
changed files with
450 additions
and 808 deletions
locomotive/steam.rb b/lib/locomotive/steam.rb
+1
-0
| @@ | @@ -20,6 +20,7 @@ require_relative 'steam/repositories/page_repository' |
| require_relative 'steam/repositories/editable_element_repository' | |
| require_relative 'steam/repositories/snippet_repository' | |
| require_relative 'steam/repositories/translation_repository' | |
| + | require_relative 'steam/repositories/theme_asset_repository' |
| require_relative 'steam/services' | |
locomotive/steam/adapters/filesystem.rb b/lib/locomotive/steam/adapters/filesystem.rb
+6
-1
| @@ | @@ -7,6 +7,7 @@ require_relative 'filesystem/yaml_loaders/site' |
| require_relative 'filesystem/yaml_loaders/page' | |
| require_relative 'filesystem/yaml_loaders/snippet' | |
| require_relative 'filesystem/yaml_loaders/translation' | |
| + | require_relative 'filesystem/yaml_loaders/theme_asset' |
| require_relative 'filesystem/sanitizer' | |
| require_relative 'filesystem/sanitizers/simple' | |
| @@ | @@ -43,6 +44,10 @@ module Locomotive::Steam |
| _query(mapper, scope) { where(_id: id) }.first | |
| end | |
| + | def theme_assets_base_url(scope) |
| + | '' |
| + | end |
| + | |
| private | |
| def _query(mapper, scope, &block) | |
| @@ | @@ -79,7 +84,7 @@ module Locomotive::Steam |
| end | |
| def build_yaml_loaders | |
| - | %i(sites pages snippets translations).inject({}) do |memo, name| |
| + | %i(sites pages snippets translations theme_assets).inject({}) do |memo, name| |
| memo[name] = build_klass('YAMLLoaders', name).new(site_path) | |
| memo | |
| end | |
locomotive/steam/adapters/filesystem/yaml_loaders/theme_asset.rb b/lib/locomotive/steam/adapters/filesystem/yaml_loaders/theme_asset.rb
+21
-0
| @@ | @@ -0,0 +1,21 @@ |
| + | module Locomotive |
| + | module Steam |
| + | module Adapters |
| + | module Filesystem |
| + | module YAMLLoaders |
| + | |
| + | class ThemeAsset |
| + | |
| + | include Adapters::Filesystem::YAMLLoader |
| + | |
| + | def load(scope) |
| + | [] |
| + | end |
| + | |
| + | end |
| + | end |
| + | end |
| + | end |
| + | end |
| + | end |
| + | |
locomotive/steam/adapters/memory/query.rb b/lib/locomotive/steam/adapters/memory/query.rb
+4
-0
| @@ | @@ -50,6 +50,10 @@ module Locomotive::Steam |
| self | |
| end | |
| + | def only(*fields) |
| + | self |
| + | end |
| + | |
| def ==(other) | |
| if other.kind_of? Array | |
| all == other | |
locomotive/steam/adapters/mongodb.rb b/lib/locomotive/steam/adapters/mongodb.rb
+4
-0
| @@ | @@ -22,6 +22,10 @@ module Locomotive::Steam |
| name.__send__(operator) | |
| end | |
| + | def theme_assets_base_url(scope) |
| + | ['', 'sites', scope.site._id.to_s, 'theme'].join('/') |
| + | end |
| + | |
| private | |
| def query_klass | |
locomotive/steam/adapters/mongodb/query.rb b/lib/locomotive/steam/adapters/mongodb/query.rb
+15
-8
| @@ | @@ -7,7 +7,7 @@ module Locomotive::Steam |
| attr_reader :criteria, :sort | |
| def initialize(scope, localized_attributes, &block) | |
| - | @criteria, @sort = {}, nil |
| + | @criteria, @sort, @fields = {}, nil, nil |
| @scope, @localized_attributes = scope, localized_attributes | |
| apply_default_scope | |
| @@ | @@ -22,22 +22,29 @@ module Locomotive::Steam |
| end | |
| def order_by(*args) | |
| - | @sort = [*args] |
| + | self.tap do |
| + | @sort = [*args] |
| + | end |
| + | end |
| + | |
| + | def only(*args) |
| + | self.tap do |
| + | @fields = [*args] |
| + | end |
| end | |
| def against(collection) | |
| _query = to_origin | |
| - | selector, sort = _query.selector, _query.options[:sort] |
| + | selector, fields, sort = _query.selector, _query.options[:fields], _query.options[:sort] |
| - | if sort |
| - | collection.find(selector).sort(sort) |
| - | else |
| - | collection.find(selector) |
| + | collection.find(selector).tap do |results| |
| + | results.sort(sort) if sort |
| + | results.select(fields) if fields |
| end | |
| end | |
| def to_origin | |
| - | build_origin_query.where(@criteria).order_by(*@sort) |
| + | build_origin_query.only(@fields).where(@criteria).order_by(*@sort) |
| end | |
| private | |
locomotive/steam/entities/theme_asset.rb b/lib/locomotive/steam/entities/theme_asset.rb
+9
-0
| @@ | @@ -0,0 +1,9 @@ |
| + | module Locomotive::Steam |
| + | |
| + | class ThemeAsset |
| + | |
| + | include Locomotive::Steam::Models::Entity |
| + | |
| + | end |
| + | |
| + | end |
locomotive/steam/repositories/filesystem/concerns/queryable.rb b/lib/locomotive/steam/repositories/filesystem/concerns/queryable.rb
+0
-65
| @@ | @@ -1,65 +0,0 @@ |
| - | # module Locomotive |
| - | # module Steam |
| - | # module Repositories |
| - | # module Filesystem |
| - | # module Concerns |
| - | |
| - | # module Queryable |
| - | |
| - | # extend ActiveSupport::Concern |
| - | |
| - | # def query(*args, &block) |
| - | # _locale = respond_to?(:current_locale) ? current_locale : nil |
| - | # MemoryAdapter::Query.new(memoized_collection(*args), _locale, &block) |
| - | # end |
| - | |
| - | # private |
| - | |
| - | # def localized_attribute(object, method) |
| - | # if (values = object.send(method)).is_a?(Hash) |
| - | # values[current_locale] |
| - | # else |
| - | # values |
| - | # end |
| - | # end |
| - | |
| - | # def memoized_collection(*args) |
| - | # return @collection if @collection |
| - | |
| - | # @collection = collection(*args) |
| - | # end |
| - | |
| - | # def collection(*args) |
| - | # _collection = loader.list_of_attributes(*args).map do |attributes| |
| - | # collection_options[:model].new(attributes) |
| - | # end |
| - | |
| - | # sanitize!(_collection) |
| - | # end |
| - | |
| - | # def sanitize!(collection) |
| - | # sanitizer.try(:apply_to, collection) || collection |
| - | # end |
| - | |
| - | # def sanitizer |
| - | # return unless (klass = collection_options[:sanitizer]) |
| - | # klass.new(site.default_locale, site.locales) |
| - | # end |
| - | |
| - | # module ClassMethods |
| - | |
| - | # def set_collection(options = {}) |
| - | # class_eval do |
| - | # define_method(:collection_options) { options } |
| - | # end |
| - | # end |
| - | |
| - | # end |
| - | |
| - | # end |
| - | |
| - | # end |
| - | # end |
| - | # end |
| - | # end |
| - | # end |
locomotive/steam/repositories/filesystem/memory_adapter/condition.rb b/lib/locomotive/steam/repositories/filesystem/memory_adapter/condition.rb
+0
-101
| @@ | @@ -1,101 +0,0 @@ |
| - | # module Locomotive |
| - | # module Steam |
| - | # module Repositories |
| - | # module Filesystem |
| - | # module MemoryAdapter |
| - | # class Condition |
| - | |
| - | # class UnsupportedOperator < StandardError; end |
| - | |
| - | # OPERATORS = %i(== eq ne neq matches gt gte lt lte size all in nin).freeze |
| - | |
| - | # attr_reader :field, :operator, :value |
| - | |
| - | # def initialize(operator_and_field, value, locale) |
| - | # @locale = locale.try(:to_sym) |
| - | # @operator_and_field, @value = operator_and_field, value |
| - | # @operator, @field = :==, operator_and_field |
| - | |
| - | # decode_operator_and_field! |
| - | # end |
| - | |
| - | # def matches?(entry) |
| - | # entry_value = entry_value(entry) |
| - | |
| - | # adapt_operator!(entry_value) |
| - | # case @operator |
| - | # when :== then entry_value == @value |
| - | # when :eq then entry_value == @value |
| - | # when :ne then entry_value != @value |
| - | # when :neq then entry_value != @value |
| - | # when :matches then @value =~ entry_value |
| - | # when :gt then entry_value > @value |
| - | # when :gte then entry_value >= @value |
| - | # when :lt then entry_value < @value |
| - | # when :lte then entry_value <= @value |
| - | # when :size then entry_value.size == @value |
| - | # when :all then array_contains?([*@value], entry_value) |
| - | # when :in, :nin then value_is_in_entry_value?(entry_value) |
| - | # else |
| - | # raise UnknownConditionInScope.new("#{@operator} is unknown or not implemented.") |
| - | # end |
| - | # end |
| - | |
| - | # def to_s |
| - | # "#{field} #{operator} #{@value.to_s}" |
| - | # end |
| - | |
| - | # protected |
| - | |
| - | # def entry_value(entry) |
| - | # case (value = entry.send(@field)) |
| - | # when Hash |
| - | # value.fetch(@locale) { nil } |
| - | # else |
| - | # value |
| - | # end |
| - | # end |
| - | |
| - | # def decode_operator_and_field! |
| - | # if match = @operator_and_field.match(/^(?<field>[a-z0-9_-]+)\.(?<operator>.*)$/) |
| - | # @field = match[:field].to_sym |
| - | # @operator = match[:operator].to_sym |
| - | # check_operator! |
| - | # end |
| - | |
| - | # @operator = :matches if @value.is_a?(Regexp) |
| - | # end |
| - | |
| - | # def adapt_operator!(value) |
| - | # case value |
| - | # when Array |
| - | # @operator = :in if @operator == :== |
| - | # end |
| - | # end |
| - | |
| - | # def value_is_in_entry_value?(value) |
| - | # _matches = if value.is_a?(Array) |
| - | # array_contains?([*value], [*@value]) |
| - | # else |
| - | # [*@value].include?(value) |
| - | # end |
| - | # @operator == :in ? _matches : !_matches |
| - | # end |
| - | |
| - | # private |
| - | |
| - | # def check_operator! |
| - | # raise UnsupportedOperator.new unless OPERATORS.include?(@operator) |
| - | # end |
| - | |
| - | # def array_contains?(source, target) |
| - | # source & target == target |
| - | # end |
| - | |
| - | # end |
| - | |
| - | # end |
| - | # end |
| - | # end |
| - | # end |
| - | # end |
locomotive/steam/repositories/filesystem/memory_adapter/query.rb b/lib/locomotive/steam/repositories/filesystem/memory_adapter/query.rb
+0
-113
| @@ | @@ -1,113 +0,0 @@ |
| - | # require 'forwardable' |
| - | |
| - | # module Locomotive |
| - | # module Steam |
| - | # module Repositories |
| - | # module Filesystem |
| - | # module MemoryAdapter |
| - | |
| - | # class Query |
| - | |
| - | # include Enumerable |
| - | # extend Forwardable |
| - | |
| - | # def_delegators :all, :each, :to_s, :to_a, :empty?, :size |
| - | |
| - | # alias :length :size |
| - | # alias :count :size |
| - | |
| - | # attr_reader :conditions |
| - | |
| - | # def initialize(dataset, locale=nil, &block) |
| - | # @dataset = dataset |
| - | # @conditions = [] |
| - | # @sorting = nil |
| - | # @limit = nil |
| - | # @offset = 0 |
| - | # @locale = locale |
| - | # instance_eval(&block) if block_given? |
| - | # end |
| - | |
| - | # def where(conditions = {}) |
| - | # @conditions += conditions.map { |name, value| Condition.new(name, value, @locale) } |
| - | # self |
| - | # end |
| - | |
| - | # def +(query) |
| - | # @conditions += query.conditions |
| - | # self |
| - | # end |
| - | |
| - | # def order_by(order_string) |
| - | # unless order_string.blank? |
| - | # pattern = order_string.include?('.') ? '.' : ' ' |
| - | # @sorting = order_string.downcase.split(pattern).map(&:to_sym) |
| - | # end |
| - | # self |
| - | # end |
| - | |
| - | # def limit(num) |
| - | # @limit = num |
| - | # self |
| - | # end |
| - | |
| - | # def offset(num) |
| - | # @offset = num |
| - | # self |
| - | # end |
| - | |
| - | # def ==(other) |
| - | # if other.kind_of? Array |
| - | # all == other |
| - | # else |
| - | # super |
| - | # end |
| - | # end |
| - | |
| - | # def all |
| - | # limited sorted(filtered) |
| - | # end |
| - | |
| - | # def sorted(entries) |
| - | # return entries if @sorting.nil? |
| - | |
| - | # name, direction = @sorting.first, (@sorting.last || :asc) |
| - | # if direction == :asc |
| - | # entries.sort { |a, b| a.send(name) <=> b.send(name) } |
| - | # else |
| - | # entries.sort { |a, b| b.send(name) <=> a.send(name) } |
| - | # end |
| - | # end |
| - | |
| - | # def limited(entries) |
| - | # return [] if @limit == 0 |
| - | # return entries if @offset == 0 && @limit.nil? |
| - | |
| - | # subentries = entries.drop(@offset || 0) |
| - | # if @limit.kind_of? Integer |
| - | # subentries.take(@limit) |
| - | # else |
| - | # subentries |
| - | # end |
| - | # end |
| - | |
| - | # def filtered |
| - | # @dataset.to_a.dup.find_all do |entry| |
| - | # accepted = true |
| - | |
| - | # @conditions.each do |_condition| |
| - | # unless _condition.matches?(entry) |
| - | # accepted = false |
| - | # break # no to go further |
| - | # end |
| - | # end |
| - | # accepted |
| - | # end |
| - | # end # filtered |
| - | |
| - | # end |
| - | # end |
| - | # end |
| - | # end |
| - | # end |
| - | # end |
locomotive/steam/repositories/filesystem/memory_adapter/simple_cache_store.rb b/lib/locomotive/steam/repositories/filesystem/memory_adapter/simple_cache_store.rb
+0
-42
| @@ | @@ -1,42 +0,0 @@ |
| - | # module Locomotive |
| - | # module Steam |
| - | # module Repositories |
| - | # module Filesystem |
| - | # module MemoryAdapter |
| - | |
| - | # class SimpleCacheStore |
| - | |
| - | # @@store = {} |
| - | |
| - | # def fetch(name, options = nil, &block) |
| - | # if block_given? |
| - | # read(name) || write(name, yield) |
| - | # else |
| - | # read(name) |
| - | # end |
| - | # end |
| - | |
| - | # def read(name, options = nil) |
| - | # @@store[name] |
| - | # end |
| - | |
| - | # def write(name, value, options = nil) |
| - | # @@store[name] = value |
| - | # end |
| - | |
| - | # def clear |
| - | # @@store.clear |
| - | # end |
| - | |
| - | # #:nocov: |
| - | # def _store |
| - | # @@store |
| - | # end |
| - | |
| - | # end |
| - | |
| - | # end |
| - | # end |
| - | # end |
| - | # end |
| - | # end |
locomotive/steam/repositories/filesystem/models/base.rb b/lib/locomotive/steam/repositories/filesystem/models/base.rb
+50
-50
| @@ | @@ -1,50 +1,50 @@ |
| - | module Locomotive |
| - | module Steam |
| - | module Repositories |
| - | module Filesystem |
| - | module Models |
| - | |
| - | class Base |
| - | |
| - | include Concerns::Validation |
| - | |
| - | attr_accessor :attributes |
| - | |
| - | def initialize(attributes) |
| - | @attributes = attributes |
| - | end |
| - | |
| - | def method_missing(name, *args, &block) |
| - | if attributes.include?(name) |
| - | self[name] |
| - | else |
| - | super |
| - | end |
| - | end |
| - | |
| - | def []=(name, value) |
| - | attributes[name.to_sym] = value |
| - | end |
| - | |
| - | def [](name) |
| - | attributes[name.to_sym] |
| - | end |
| - | |
| - | def self.set_localized_attributes(list) |
| - | singleton = class << self; self; end |
| - | singleton.class_eval do |
| - | define_method(:localized_attributes) { list } |
| - | end |
| - | |
| - | class_eval do |
| - | define_method(:localized_attributes) { list } |
| - | end |
| - | end |
| - | |
| - | end |
| - | |
| - | end |
| - | end |
| - | end |
| - | end |
| - | end |
| + | # module Locomotive |
| + | # module Steam |
| + | # module Repositories |
| + | # module Filesystem |
| + | # module Models |
| + | |
| + | # class Base |
| + | |
| + | # include Concerns::Validation |
| + | |
| + | # attr_accessor :attributes |
| + | |
| + | # def initialize(attributes) |
| + | # @attributes = attributes |
| + | # end |
| + | |
| + | # def method_missing(name, *args, &block) |
| + | # if attributes.include?(name) |
| + | # self[name] |
| + | # else |
| + | # super |
| + | # end |
| + | # end |
| + | |
| + | # def []=(name, value) |
| + | # attributes[name.to_sym] = value |
| + | # end |
| + | |
| + | # def [](name) |
| + | # attributes[name.to_sym] |
| + | # end |
| + | |
| + | # def self.set_localized_attributes(list) |
| + | # singleton = class << self; self; end |
| + | # singleton.class_eval do |
| + | # define_method(:localized_attributes) { list } |
| + | # end |
| + | |
| + | # class_eval do |
| + | # define_method(:localized_attributes) { list } |
| + | # end |
| + | # end |
| + | |
| + | # end |
| + | |
| + | # end |
| + | # end |
| + | # end |
| + | # end |
| + | # end |
locomotive/steam/repositories/filesystem/models/content_entry.rb b/lib/locomotive/steam/repositories/filesystem/models/content_entry.rb
+145
-145
| @@ | @@ -1,145 +1,145 @@ |
| - | require 'chronic' |
| - | |
| - | module Locomotive |
| - | module Steam |
| - | module Repositories |
| - | module Filesystem |
| - | module Models |
| - | |
| - | class ContentEntry < Base |
| - | |
| - | ASSOCIATION_NAMES = [:belongs_to, :has_many, :many_to_many].freeze |
| - | |
| - | set_localized_attributes [:_id, :_slug, :seo_title, :meta_description, :meta_keywords] |
| - | |
| - | attr_accessor :content_type |
| - | |
| - | def initialize(attributes = {}) |
| - | super({ |
| - | _visible: true, |
| - | _position: 0, |
| - | created_at: Time.now, |
| - | updated_at: Time.now |
| - | }.merge(attributes)) |
| - | end |
| - | |
| - | def _slug; self[:_slug]; end |
| - | alias :_id :_slug |
| - | alias :_permalink :_slug |
| - | |
| - | def method_missing(name, *args, &block) |
| - | if is_dynamic_attribute?(name) |
| - | cast_value(name) |
| - | elsif attributes.include?(name) |
| - | self[name] |
| - | else |
| - | super |
| - | end |
| - | end |
| - | |
| - | def valid? |
| - | errors.clear |
| - | content_type.fields_by_name.each do |name, field| |
| - | next unless field.required? |
| - | errors.add_on_blank(name.to_sym) |
| - | end |
| - | errors.empty? |
| - | end |
| - | |
| - | def content_type |
| - | @content_type || attributes[:content_type] |
| - | end |
| - | |
| - | def content_type_slug |
| - | content_type.slug |
| - | end |
| - | |
| - | def _label |
| - | self[content_type.label_field_name] |
| - | end |
| - | |
| - | def localized_attributes |
| - | self.class.localized_attributes + content_type.localized_fields_names |
| - | end |
| - | |
| - | def to_liquid |
| - | Locomotive::Steam::Liquid::Drops::ContentEntry.new(self) |
| - | end |
| - | |
| - | private |
| - | |
| - | def is_dynamic_attribute?(name) |
| - | content_type.fields_by_name.has_key?(name) |
| - | end |
| - | |
| - | def cast_value(name) |
| - | field = content_type.fields_by_name[name] |
| - | |
| - | begin |
| - | _cast_value(field) |
| - | rescue Exception => e |
| - | Locomotive::Common::Logger.info "[#{content_type.slug}][#{_label}] Unable to cast the \"#{name}\" field, reason: #{e.message}".yellow |
| - | nil |
| - | end |
| - | end |
| - | |
| - | def _cast_value(field) |
| - | if ASSOCIATION_NAMES.include?(field.type) |
| - | AssociationMetadata.new(field.type, self, field, [*attributes[field.name]]) |
| - | elsif private_methods.include?(:"_cast_#{field.type}") |
| - | send(:"_cast_#{field.type}", field.name) |
| - | else |
| - | attributes[field.name] |
| - | end |
| - | end |
| - | |
| - | def _cast_integer(name) |
| - | _cast_convertor(name, &:to_i) |
| - | end |
| - | |
| - | def _cast_float(name) |
| - | _cast_convertor(name, &:to_f) |
| - | end |
| - | |
| - | def _cast_file(name) |
| - | _cast_convertor(name) do |value| |
| - | value.present? ? { 'url' => value } : nil |
| - | end |
| - | end |
| - | |
| - | def _cast_date(name) |
| - | _cast_time(name, :to_date) |
| - | end |
| - | |
| - | def _cast_date_time(name) |
| - | _cast_time(name, :to_date) |
| - | end |
| - | |
| - | def _cast_time(name, end_method) |
| - | _cast_convertor(name) do |value| |
| - | value.is_a?(String) ? Chronic.parse(value).send(end_method) : value |
| - | end |
| - | end |
| - | |
| - | def _cast_convertor(name, &block) |
| - | if (value = attributes[name]).is_a?(Hash) |
| - | value.each { |l, _value| value[l] = yield(_value) } |
| - | else |
| - | yield(value) |
| - | end |
| - | end |
| - | |
| - | class AssociationMetadata < Struct.new(:type, :source, :field, :target_slugs) |
| - | def association; true; end |
| - | def inverse_of; field.inverse_of; end |
| - | def target_class_slug; field.class_name; end |
| - | def order_by; field[:order_by]; end |
| - | end |
| - | |
| - | end |
| - | |
| - | end |
| - | end |
| - | end |
| - | end |
| - | end |
| + | # require 'chronic' |
| + | |
| + | # module Locomotive |
| + | # module Steam |
| + | # module Repositories |
| + | # module Filesystem |
| + | # module Models |
| + | |
| + | # class ContentEntry < Base |
| + | |
| + | # ASSOCIATION_NAMES = [:belongs_to, :has_many, :many_to_many].freeze |
| + | |
| + | # set_localized_attributes [:_id, :_slug, :seo_title, :meta_description, :meta_keywords] |
| + | |
| + | # attr_accessor :content_type |
| + | |
| + | # def initialize(attributes = {}) |
| + | # super({ |
| + | # _visible: true, |
| + | # _position: 0, |
| + | # created_at: Time.now, |
| + | # updated_at: Time.now |
| + | # }.merge(attributes)) |
| + | # end |
| + | |
| + | # def _slug; self[:_slug]; end |
| + | # alias :_id :_slug |
| + | # alias :_permalink :_slug |
| + | |
| + | # def method_missing(name, *args, &block) |
| + | # if is_dynamic_attribute?(name) |
| + | # cast_value(name) |
| + | # elsif attributes.include?(name) |
| + | # self[name] |
| + | # else |
| + | # super |
| + | # end |
| + | # end |
| + | |
| + | # def valid? |
| + | # errors.clear |
| + | # content_type.fields_by_name.each do |name, field| |
| + | # next unless field.required? |
| + | # errors.add_on_blank(name.to_sym) |
| + | # end |
| + | # errors.empty? |
| + | # end |
| + | |
| + | # def content_type |
| + | # @content_type || attributes[:content_type] |
| + | # end |
| + | |
| + | # def content_type_slug |
| + | # content_type.slug |
| + | # end |
| + | |
| + | # def _label |
| + | # self[content_type.label_field_name] |
| + | # end |
| + | |
| + | # def localized_attributes |
| + | # self.class.localized_attributes + content_type.localized_fields_names |
| + | # end |
| + | |
| + | # def to_liquid |
| + | # Locomotive::Steam::Liquid::Drops::ContentEntry.new(self) |
| + | # end |
| + | |
| + | # private |
| + | |
| + | # def is_dynamic_attribute?(name) |
| + | # content_type.fields_by_name.has_key?(name) |
| + | # end |
| + | |
| + | # def cast_value(name) |
| + | # field = content_type.fields_by_name[name] |
| + | |
| + | # begin |
| + | # _cast_value(field) |
| + | # rescue Exception => e |
| + | # Locomotive::Common::Logger.info "[#{content_type.slug}][#{_label}] Unable to cast the \"#{name}\" field, reason: #{e.message}".yellow |
| + | # nil |
| + | # end |
| + | # end |
| + | |
| + | # def _cast_value(field) |
| + | # if ASSOCIATION_NAMES.include?(field.type) |
| + | # AssociationMetadata.new(field.type, self, field, [*attributes[field.name]]) |
| + | # elsif private_methods.include?(:"_cast_#{field.type}") |
| + | # send(:"_cast_#{field.type}", field.name) |
| + | # else |
| + | # attributes[field.name] |
| + | # end |
| + | # end |
| + | |
| + | # def _cast_integer(name) |
| + | # _cast_convertor(name, &:to_i) |
| + | # end |
| + | |
| + | # def _cast_float(name) |
| + | # _cast_convertor(name, &:to_f) |
| + | # end |
| + | |
| + | # def _cast_file(name) |
| + | # _cast_convertor(name) do |value| |
| + | # value.present? ? { 'url' => value } : nil |
| + | # end |
| + | # end |
| + | |
| + | # def _cast_date(name) |
| + | # _cast_time(name, :to_date) |
| + | # end |
| + | |
| + | # def _cast_date_time(name) |
| + | # _cast_time(name, :to_date) |
| + | # end |
| + | |
| + | # def _cast_time(name, end_method) |
| + | # _cast_convertor(name) do |value| |
| + | # value.is_a?(String) ? Chronic.parse(value).send(end_method) : value |
| + | # end |
| + | # end |
| + | |
| + | # def _cast_convertor(name, &block) |
| + | # if (value = attributes[name]).is_a?(Hash) |
| + | # value.each { |l, _value| value[l] = yield(_value) } |
| + | # else |
| + | # yield(value) |
| + | # end |
| + | # end |
| + | |
| + | # class AssociationMetadata < Struct.new(:type, :source, :field, :target_slugs) |
| + | # def association; true; end |
| + | # def inverse_of; field.inverse_of; end |
| + | # def target_class_slug; field.class_name; end |
| + | # def order_by; field[:order_by]; end |
| + | # end |
| + | |
| + | # end |
| + | |
| + | # end |
| + | # end |
| + | # end |
| + | # end |
| + | # end |
locomotive/steam/repositories/filesystem/models/content_type.rb b/lib/locomotive/steam/repositories/filesystem/models/content_type.rb
+41
-41
| @@ | @@ -1,41 +1,41 @@ |
| - | module Locomotive |
| - | module Steam |
| - | module Repositories |
| - | module Filesystem |
| - | module Models |
| - | |
| - | class ContentType < Base |
| - | |
| - | attr_accessor :fields, :fields_by_name |
| - | |
| - | def initialize(attributes = {}) |
| - | super({ |
| - | order_by: '_position', |
| - | order_direction: 'asc' |
| - | }.merge(attributes)) |
| - | end |
| - | |
| - | def label_field_name |
| - | (self[:label_field_name] || fields.first.name).to_sym |
| - | end |
| - | |
| - | def localized_fields_names |
| - | query_fields { where(localized: true) }.all.map(&:name) |
| - | end |
| - | |
| - | def order_by |
| - | name = self[:order_by] == 'manually' ? '_position' : self[:order_by] |
| - | "#{name} #{self.order_direction}" |
| - | end |
| - | |
| - | def query_fields(&block) |
| - | Filesystem::MemoryAdapter::Query.new(fields, &block) |
| - | end |
| - | |
| - | end |
| - | |
| - | end |
| - | end |
| - | end |
| - | end |
| - | end |
| + | # module Locomotive |
| + | # module Steam |
| + | # module Repositories |
| + | # module Filesystem |
| + | # module Models |
| + | |
| + | # class ContentType < Base |
| + | |
| + | # attr_accessor :fields, :fields_by_name |
| + | |
| + | # def initialize(attributes = {}) |
| + | # super({ |
| + | # order_by: '_position', |
| + | # order_direction: 'asc' |
| + | # }.merge(attributes)) |
| + | # end |
| + | |
| + | # def label_field_name |
| + | # (self[:label_field_name] || fields.first.name).to_sym |
| + | # end |
| + | |
| + | # def localized_fields_names |
| + | # query_fields { where(localized: true) }.all.map(&:name) |
| + | # end |
| + | |
| + | # def order_by |
| + | # name = self[:order_by] == 'manually' ? '_position' : self[:order_by] |
| + | # "#{name} #{self.order_direction}" |
| + | # end |
| + | |
| + | # def query_fields(&block) |
| + | # Filesystem::MemoryAdapter::Query.new(fields, &block) |
| + | # end |
| + | |
| + | # end |
| + | |
| + | # end |
| + | # end |
| + | # end |
| + | # end |
| + | # end |
locomotive/steam/repositories/filesystem/models/content_type_field.rb b/lib/locomotive/steam/repositories/filesystem/models/content_type_field.rb
+25
-25
| @@ | @@ -1,31 +1,31 @@ |
| - | module Locomotive |
| - | module Steam |
| - | module Repositories |
| - | module Filesystem |
| - | module Models |
| + | # module Locomotive |
| + | # module Steam |
| + | # module Repositories |
| + | # module Filesystem |
| + | # module Models |
| - | class ContentTypeField < Base |
| + | # class ContentTypeField < Base |
| - | def initialize(attributes) |
| - | super({ |
| - | type: :string, |
| - | localized: false, |
| - | required: false, |
| - | unique: false |
| - | }.merge(attributes)) |
| - | end |
| + | # def initialize(attributes) |
| + | # super({ |
| + | # type: :string, |
| + | # localized: false, |
| + | # required: false, |
| + | # unique: false |
| + | # }.merge(attributes)) |
| + | # end |
| - | def class_name |
| - | self[:class_name] || self[:target] |
| - | end |
| + | # def class_name |
| + | # self[:class_name] || self[:target] |
| + | # end |
| - | def required?; self[:required]; end |
| - | def localized?; self[:localized]; end |
| + | # def required?; self[:required]; end |
| + | # def localized?; self[:localized]; end |
| - | end |
| + | # end |
| - | end |
| - | end |
| - | end |
| - | end |
| - | end |
| + | # end |
| + | # end |
| + | # end |
| + | # end |
| + | # end |
locomotive/steam/repositories/filesystem/models/editable_element.rb b/lib/locomotive/steam/repositories/filesystem/models/editable_element.rb
+0
-14
| @@ | @@ -1,14 +0,0 @@ |
| - | module Locomotive |
| - | module Steam |
| - | module Repositories |
| - | module Filesystem |
| - | module Models |
| - | |
| - | class EditableElement < Struct.new(:block, :slug, :content) |
| - | end |
| - | |
| - | end |
| - | end |
| - | end |
| - | end |
| - | end |
locomotive/steam/repositories/filesystem/models/site.rb b/lib/locomotive/steam/repositories/filesystem/models/site.rb
+0
-38
| @@ | @@ -1,38 +0,0 @@ |
| - | # module Locomotive |
| - | # module Steam |
| - | # module Repositories |
| - | # module Filesystem |
| - | # module Models |
| - | |
| - | # class Site < Base |
| - | |
| - | # set_localized_attributes [:seo_title, :meta_description, :meta_keywords] |
| - | |
| - | # attr_accessor :root_path |
| - | |
| - | # def initialize(attributes = {}) |
| - | # super({ |
| - | # timezone: 'UTC', |
| - | # prefix_default_locale: false |
| - | # }.merge(attributes)) |
| - | # end |
| - | |
| - | # def default_locale |
| - | # self.locales.try(:first) || :en |
| - | # end |
| - | |
| - | # def locales |
| - | # attributes[:locales].map(&:to_sym) |
| - | # end |
| - | |
| - | # def to_liquid |
| - | # Steam::Liquid::Drops::Site.new(self) |
| - | # end |
| - | |
| - | # end |
| - | |
| - | # end |
| - | # end |
| - | # end |
| - | # end |
| - | # end |
locomotive/steam/repositories/filesystem/models/snippet.rb b/lib/locomotive/steam/repositories/filesystem/models/snippet.rb
+0
-21
| @@ | @@ -1,21 +0,0 @@ |
| - | # module Locomotive |
| - | # module Steam |
| - | # module Repositories |
| - | # module Filesystem |
| - | # module Models |
| - | |
| - | # class Snippet < Base |
| - | |
| - | # set_localized_attributes [:template, :template_path] |
| - | |
| - | # def initialize(attributes) |
| - | # super({ template: {} }.merge(attributes)) |
| - | # end |
| - | |
| - | # end |
| - | |
| - | # end |
| - | # end |
| - | # end |
| - | # end |
| - | # end |
locomotive/steam/repositories/filesystem/models/translation.rb b/lib/locomotive/steam/repositories/filesystem/models/translation.rb
+0
-19
| @@ | @@ -1,19 +0,0 @@ |
| - | module Locomotive |
| - | module Steam |
| - | module Repositories |
| - | module Filesystem |
| - | module Models |
| - | |
| - | class Translation < Base |
| - | |
| - | def values |
| - | self[:values] |
| - | end |
| - | |
| - | end |
| - | |
| - | end |
| - | end |
| - | end |
| - | end |
| - | end |
locomotive/steam/repositories/filesystem/theme_asset.rb b/lib/locomotive/steam/repositories/filesystem/theme_asset.rb
+0
-23
| @@ | @@ -1,23 +0,0 @@ |
| - | # module Locomotive |
| - | # module Steam |
| - | # module Repositories |
| - | # module Filesystem |
| - | |
| - | # class ThemeAsset < Struct.new(:site) |
| - | |
| - | # # Engine: ['', 'sites', site._id.to_s, 'theme', path].join('/') |
| - | # def url_for(path) |
| - | # '/' + path |
| - | # end |
| - | |
| - | # # Engine: site.theme_assets.checksums |
| - | # def checksums |
| - | # {} |
| - | # end |
| - | |
| - | # end |
| - | |
| - | # end |
| - | # end |
| - | # end |
| - | # end |
locomotive/steam/repositories/filesystem/translation.rb b/lib/locomotive/steam/repositories/filesystem/translation.rb
+0
-22
| @@ | @@ -1,22 +0,0 @@ |
| - | # module Locomotive |
| - | # module Steam |
| - | # module Repositories |
| - | # module Filesystem |
| - | |
| - | # class Translation < Struct.new(:loader) |
| - | |
| - | # include Concerns::Queryable |
| - | |
| - | # set_collection model: Filesystem::Models::Translation |
| - | |
| - | # # Engine: site.translations.where(key: key).first |
| - | # def find(key) |
| - | # query { where(key: key) }.first |
| - | # end |
| - | |
| - | # end |
| - | |
| - | # end |
| - | # end |
| - | # end |
| - | # end |
locomotive/steam/repositories/filesystem/yaml_loaders/translation.rb b/lib/locomotive/steam/repositories/filesystem/yaml_loaders/translation.rb
+0
-35
| @@ | @@ -1,35 +0,0 @@ |
| - | # module Locomotive |
| - | # module Steam |
| - | # module Repositories |
| - | # module Filesystem |
| - | # module YAMLLoaders |
| - | |
| - | # class Translation < Struct.new(:root_path, :cache) |
| - | |
| - | # include YAMLLoaders::Concerns::Common |
| - | |
| - | # def list_of_attributes |
| - | # cache.fetch('config/translations') { load_array } |
| - | # end |
| - | |
| - | # private |
| - | |
| - | # def load_array |
| - | # [].tap do |array| |
| - | # load(path).each do |key, values| |
| - | # array << { key: key.to_s, values: HashConverter.to_string(values) } |
| - | # end |
| - | # end |
| - | # end |
| - | |
| - | # def path |
| - | # File.join(root_path, 'config', 'translations.yml') |
| - | # end |
| - | |
| - | # end |
| - | |
| - | # end |
| - | # end |
| - | # end |
| - | # end |
| - | # end |
locomotive/steam/repositories/theme_asset_repository.rb b/lib/locomotive/steam/repositories/theme_asset_repository.rb
+27
-0
| @@ | @@ -0,0 +1,27 @@ |
| + | module Locomotive |
| + | module Steam |
| + | |
| + | class ThemeAssetRepository |
| + | |
| + | include Models::Repository |
| + | |
| + | mapping :theme_assets, entity: ThemeAsset |
| + | |
| + | # Engine: ['', 'sites', site._id.to_s, 'theme', path].join('/') |
| + | # Wagon: '/' + path |
| + | def url_for(path) |
| + | [adapter.theme_assets_base_url(scope), path].join('/') |
| + | end |
| + | |
| + | # Engine: site.theme_assets.checksums |
| + | # Wagon: {} |
| + | def checksums |
| + | query { only(:checksum, :local_path) }.all.inject({}) do |memo, asset| |
| + | memo[asset.local_path] = asset.checksum |
| + | memo |
| + | end |
| + | end |
| + | |
| + | end |
| + | end |
| + | end |
locomotive/steam/repositories/translation_repository.rb b/lib/locomotive/steam/repositories/translation_repository.rb
+1
-3
| @@ | @@ -5,9 +5,7 @@ module Locomotive |
| include Models::Repository | |
| - | mapping :translations, entity: Translation do |
| - | localized_attributes :template_path, :template |
| - | end |
| + | mapping :translations, entity: Translation |
| def by_key(key) | |
| query { where(key: key) }.first | |
spec/integration/mongodb_helper.rb
+3
-3
| @@ | @@ -1,5 +1,5 @@ |
| require File.join(File.dirname(__FILE__), '..', 'spec_helper') | |
| - | RSpec.configure do |config| |
| - | config.before(:all) { restore_mongodb } |
| - | end |
| + | # RSpec.configure do |config| |
| + | # config.before(:all) { restore_mongodb } |
| + | # end |
spec/integration/repositories/theme_asset_repository_spec.rb
+48
-0
| @@ | @@ -0,0 +1,48 @@ |
| + | require File.join(File.dirname(__FILE__), '..', 'mongodb_helper') |
| + | |
| + | require_relative '../../../lib/locomotive/steam/adapters/filesystem.rb' |
| + | require_relative '../../../lib/locomotive/steam/adapters/mongodb.rb' |
| + | |
| + | describe Locomotive::Steam::ThemeAssetRepository do |
| + | |
| + | let(:site) { Locomotive::Steam::Site.new(_id: site_id, locales: %w(en fr nb)) } |
| + | let(:repository) { described_class.new(adapter, site, :en) } |
| + | |
| + | context 'MongoDB' do |
| + | |
| + | let(:site_id) { BSON::ObjectId.from_string('54eb49c12475804b2b000002') } |
| + | let(:adapter) { Locomotive::Steam::MongoDBAdapter.new('steam_test', ['127.0.0.1:27017']) } |
| + | |
| + | describe '#url_for' do |
| + | subject { repository.url_for('stylesheets/application.css') } |
| + | it { is_expected.to eq '/sites/54eb49c12475804b2b000002/theme/stylesheets/application.css' } |
| + | end |
| + | |
| + | describe '#checksums' do |
| + | subject { repository.checksums } |
| + | it { expect(subject.size).to eq 16 } |
| + | it { expect(subject['stylesheets/application.css']).to eq 'aa017461d702a80ef8837e51e65deb4f' } |
| + | end |
| + | |
| + | end |
| + | |
| + | context 'Filesystem' do |
| + | |
| + | let(:site_id) { 1 } |
| + | let(:adapter) { Locomotive::Steam::FilesystemAdapter.new(default_fixture_site_path) } |
| + | |
| + | describe '#url_for' do |
| + | subject { repository.url_for('stylesheets/application.css') } |
| + | it { is_expected.to eq '/stylesheets/application.css' } |
| + | end |
| + | |
| + | describe '#checksums' do |
| + | subject { repository.checksums } |
| + | it { expect(subject).to eq({}) } |
| + | end |
| + | |
| + | after(:all) { Locomotive::Steam::Adapters::Filesystem::SimpleCacheStore.new.clear } |
| + | |
| + | end |
| + | |
| + | end |
spec/unit/adapters/memory/query_spec.rb
+13
-3
| @@ | @@ -7,9 +7,9 @@ require_relative '../../../../lib/locomotive/steam/adapters/memory/query.rb' |
| describe Locomotive::Steam::Adapters::Memory::Query do | |
| - | let(:entry_1) { OpenStruct.new(name: 'foo', id: 1) } |
| - | let(:entry_2) { OpenStruct.new(name: 'bar', id: 2) } |
| - | let(:entry_3) { OpenStruct.new(name: 'zone', id: 3) } |
| + | let(:entry_1) { OpenStruct.new(name: 'foo', id: 1, attributes: { name: 'foo', id: 1 }) } |
| + | let(:entry_2) { OpenStruct.new(name: 'bar', id: 2, attributes: { name: 'bar', id: 2 }) } |
| + | let(:entry_3) { OpenStruct.new(name: 'zone', id: 3, attributes: { name: 'zone', id: 3 }) } |
| let(:records) { { 1 => entry_1, 2 => entry_2, 3 => entry_3 } } | |
| let(:dataset) { Locomotive::Steam::Adapters::Memory::Dataset.new(:test) } | |
| let(:locale) { :en } | |
| @@ | @@ -18,6 +18,16 @@ describe Locomotive::Steam::Adapters::Memory::Query do |
| before { allow(dataset).to receive(:records).and_return(records) } | |
| + | describe '#only' do |
| + | specify do |
| + | expect( |
| + | query.new(dataset, locale) do |
| + | only(:name) |
| + | end.all.map(&:name) |
| + | ).to eq(['foo', 'bar', 'zone']) |
| + | end |
| + | end |
| + | |
| describe '#limited' do | |
| specify do | |
| expect( | |
query_spec.rb b/spec/unit/adapters/mongodb/query_spec.rb
+2
-1
| @@ | @@ -55,12 +55,13 @@ describe Locomotive::Steam::Adapters::MongoDB::Query do |
| describe '#to_origin' do | |
| - | before { query.where(:title.in => %w(index)).order_by(title: :asc) } |
| + | before { query.where(:title.in => %w(index)).only(:title, :published).order_by(title: :asc) } |
| subject { query.to_origin } | |
| it { expect(subject.selector).to eq({ 'site_id' => 42, 'title.en' => { '$in' => %w(index) } }) } | |
| it { expect(subject.options[:sort]).to eq({ 'title.en' => 1 }) } | |
| + | it { expect(subject.options[:fields]).to eq({ 'title.en' => 1, 'published' => 1}) } |
| end | |
spec/unit/repositories/filesystem/translation_spec.rb
+0
-35
| @@ | @@ -1,35 +0,0 @@ |
| - | # require 'spec_helper' |
| - | |
| - | # describe Locomotive::Steam::Repositories::Filesystem::Translation do |
| - | |
| - | # let(:loader) { instance_double('Loader', list_of_attributes: [{ key: 'powered_by', values: { 'en' => 'Powered by Steam', 'fr' => 'Propulsé par Steam' } }]) } |
| - | # let(:locale) { :en } |
| - | |
| - | # let(:repository) { Locomotive::Steam::Repositories::Filesystem::Translation.new(loader) } |
| - | |
| - | # describe '#collection' do |
| - | |
| - | # subject { repository.send(:collection).first } |
| - | |
| - | # it { expect(subject.class).to eq Locomotive::Steam::Repositories::Filesystem::Models::Translation } |
| - | # it { expect(subject.key).to eq 'powered_by' } |
| - | |
| - | # end |
| - | |
| - | # describe '#find' do |
| - | |
| - | # let(:key) { nil } |
| - | # subject { repository.find(key) } |
| - | |
| - | # it { is_expected.to eq nil } |
| - | |
| - | # context 'existing translation' do |
| - | |
| - | # let(:key) { 'powered_by' } |
| - | # it { expect(subject.values).to eq({ 'en' => 'Powered by Steam', 'fr' => 'Propulsé par Steam' }) } |
| - | |
| - | # end |
| - | |
| - | # end |
| - | |
| - | # end |
spec/unit/repositories/theme_asset_repository_spec.rb
+35
-0
| @@ | @@ -0,0 +1,35 @@ |
| + | require 'spec_helper' |
| + | |
| + | require_relative '../../../lib/locomotive/steam/adapters/filesystem.rb' |
| + | |
| + | describe Locomotive::Steam::ThemeAssetRepository do |
| + | |
| + | let(:theme_assets) { [{ local_path: 'application.css', checksum: 42 }] } |
| + | let(:locale) { :en } |
| + | let(:site) { instance_double('Site', _id: 1, default_locale: :en, locales: [:en, :fr]) } |
| + | let(:adapter) { Locomotive::Steam::FilesystemAdapter.new(nil) } |
| + | let(:repository) { described_class.new(adapter, site, locale) } |
| + | |
| + | before do |
| + | allow(adapter).to receive(:collection).and_return(theme_assets) |
| + | adapter.cache = NoCacheStore.new |
| + | end |
| + | |
| + | describe '#url_for' do |
| + | |
| + | let(:path) { 'main.css' } |
| + | subject { repository.url_for(path) } |
| + | |
| + | it { is_expected.to eq '/main.css' } |
| + | |
| + | end |
| + | |
| + | describe '#checksums' do |
| + | |
| + | subject { repository.checksums } |
| + | |
| + | it { is_expected.to eq({ 'application.css' => 42 }) } |
| + | |
| + | end |
| + | |
| + | end |